home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / gccdif~1.z / gccdif~1 / combine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-30  |  85.3 KB  |  2,749 lines

  1. /* Optimize by combining instructions for GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This module is essentially the "combiner" phase of the U. of Arizona
  22.    Portable Optimizer, but redone to work on our list-structured
  23.    representation for RTL instead of their string representation.
  24.  
  25.    The LOG_LINKS of each insn identify the most recent assignment
  26.    to each REG used in the insn.  It is a list of previous insns,
  27.    each of which contains a SET for a REG that is used in this insn
  28.    and not used or set in between.  LOG_LINKs never cross basic blocks.
  29.    They were set up by the preceding pass (lifetime analysis).
  30.  
  31.    We try to combine each pair of insns joined by a logical link.
  32.    We also try to combine triples of insns A, B and C when
  33.    C has a link back to B and B has a link back to A.
  34.  
  35.    LOG_LINKS does not have links for use of the CC0.  They don't
  36.    need to, because the insn that sets the CC0 is always immediately
  37.    before the insn that tests it.  So we always regard a branch
  38.    insn as having a logical link to the preceding insn.
  39.  
  40.    We check (with use_crosses_set_p) to avoid combining in such a way
  41.    as to move a computation to a place where its value would be different.
  42.  
  43.    Combination is done by mathematically substituting the previous
  44.    insn(s) values for the regs they set into the expressions in
  45.    the later insns that refer to these regs.  If the result is a valid insn
  46.    for our target machine, according to the machine description,
  47.    we install it, delete the earlier insns, and update the data flow
  48.    information (LOG_LINKS and REG_NOTES) for what we did.
  49.  
  50.    To simplify substitution, we combine only when the earlier insn(s)
  51.    consist of only a single assignment.  To simplify updating afterward,
  52.    we never combine when a subroutine call appears in the middle.
  53.  
  54.    Since we do not represent assignments to CC0 explicitly except when that
  55.    is all an insn does, there is no LOG_LINKS entry in an insn that uses
  56.    the condition code for the insn that set the condition code.
  57.    Fortunately, these two insns must be consecutive.
  58.    Therefore, every JUMP_INSN is taken to have an implicit logical link
  59.    to the preceding insn.  This is not quite right, since non-jumps can
  60.    also use the condition code; but in practice such insns would not
  61.    combine anyway.  */
  62.  
  63. #include <stdio.h>
  64.  
  65. #include "config.h"
  66. #include "rtl.h"
  67. #include "flags.h"
  68. #include "regs.h"
  69. #include "basic-block.h"
  70. #include "insn-config.h"
  71. #include "recog.h"
  72.  
  73. #define max(A,B) ((A) > (B) ? (A) : (B))
  74. #define min(A,B) ((A) < (B) ? (A) : (B))
  75.  
  76. /* It is not safe to use ordinary gen_lowpart in combine.
  77.    Use gen_lowpart_for_combine instead.  See comments there.  */
  78. #define gen_lowpart dont_use_gen_lowpart_you_dummy
  79.  
  80. /* Number of attempts to combine instructions in this function.  */
  81.  
  82. static int combine_attempts;
  83. static int distrib_attempts;
  84.  
  85. /* Number of attempts that got as far as substitution in this function.  */
  86.  
  87. static int combine_merges;
  88. static int distrib_merges_1, distrib_merges_2;
  89.  
  90. /* Number of instructions combined with added SETs in this function.  */
  91.  
  92. static int combine_extras;
  93.  
  94. /* Number of instructions combined in this function.  */
  95.  
  96. static int combine_successes;
  97. static int distrib_successes;
  98.  
  99. /* Totals over entire compilation.  */
  100.  
  101. static int total_attempts, total_merges, total_extras, total_successes;
  102. static int total_distrib_attempts, total_distrib_merges_1, total_distrib_merges_2, total_distrib_successes;
  103.  
  104.  
  105. /* Vector mapping INSN_UIDs to cuids.
  106.    The cuids are like uids but increase monononically always.
  107.    Combine always uses cuids so that it can compare them.
  108.    But actually renumbering the uids, which we used to do,
  109.    proves to be a bad idea because it makes it hard to compare
  110.    the dumps produced by earlier passes with those from later passes.  */
  111.  
  112. static short *uid_cuid;
  113.  
  114. /* Get the cuid of an insn.  */
  115.  
  116. #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
  117.  
  118.  
  119. /* Record last point of death of (hard or pseudo) register n.  */
  120.  
  121. static rtx *reg_last_death;
  122.  
  123. /* Record last point of modification of (hard or pseudo) register n.  */
  124.  
  125. static rtx *reg_last_set;
  126.  
  127. /* Record the cuid of the last insn that invalidated memory
  128.    (anything that writes memory, and subroutine calls).  */
  129.  
  130. static int mem_last_set;
  131.  
  132. /* Record the cuid of the last CALL_INSN
  133.    so we can tell whether a potential combination crosses any calls.  */
  134.  
  135. static int last_call_cuid;
  136.  
  137. /* When `subst' is called, this is the insn that is being modified
  138.    (by combining in a previous insn).  The PATTERN of this insn
  139.    is still the old pattern partially modified and it should not be
  140.    looked at, but this may be used to examine the successors of the insn
  141.    to judge whether a simplification is valid.  */
  142.  
  143. static rtx subst_insn;
  144.  
  145. /* Record one modification to rtl structure
  146.    to be undone by storing old_contents into *where.
  147.    is_int is 1 if the contents are an int.  */
  148.  
  149. struct undo
  150. {
  151.   rtx *where;
  152.   rtx old_contents;
  153.   int is_int;
  154. };
  155.  
  156. struct undo_int
  157. {
  158.   int *where;
  159.   int old_contents;
  160.   int is_int;
  161. };
  162.  
  163. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  164.    num_undo says how many are currently recorded.
  165.    storage is nonzero if we must undo the allocation of new storage.
  166.    The value of storage is what to pass to obfree.  */
  167.  
  168. #define MAX_UNDO 10
  169.  
  170. struct undobuf
  171. {
  172.   int num_undo;
  173.   char *storage;
  174.   struct undo undo[MAX_UNDO];
  175. };
  176.  
  177. static struct undobuf undobuf;
  178.  
  179. /* Number of times the pseudo being substituted for
  180.    was found and replaced.  */
  181.  
  182. static int n_occurrences;
  183.  
  184. static void move_deaths ();
  185. static void move_deaths_2 ();
  186. void remove_death ();
  187. static void record_dead_and_set_regs ();
  188. int regno_dead_p ();
  189. static int use_crosses_set_p ();
  190. static int try_combine ();
  191. static rtx try_distrib ();
  192. static rtx subst ();
  193. static void undo_all ();
  194. static void copy_substitutions ();
  195. static void add_links ();
  196. static void remove_links ();
  197. static void add_incs ();
  198. static int adjacent_insns_p ();
  199. static int check_asm_operands ();
  200. static rtx simplify_and_const_int ();
  201. static rtx gen_lowpart_for_combine ();
  202. static void simplify_set_cc0_and ();
  203.  
  204. /* Main entry point for combiner.  F is the first insn of the function.
  205.    NREGS is the first unused pseudo-reg number.  */
  206.  
  207. void
  208. combine_instructions (f, nregs)
  209.      rtx f;
  210.      int nregs;
  211. {
  212.   register rtx insn;
  213.   register int i;
  214.   register rtx links, nextlinks;
  215.   rtx prev;
  216.  
  217.   combine_attempts = 0;
  218.   combine_merges = 0;
  219.   combine_extras = 0;
  220.   combine_successes = 0;
  221.   distrib_attempts = 0;
  222.   distrib_merges_1 = 0;
  223.   distrib_merges_2 = 0;
  224.   distrib_successes = 0;
  225.  
  226.   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
  227.   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
  228.   bzero (reg_last_death, nregs * sizeof (rtx));
  229.   bzero (reg_last_set, nregs * sizeof (rtx));
  230.  
  231.   init_recog ();
  232.  
  233.   /* Compute maximum uid value so uid_cuid can be allocated.  */
  234.  
  235.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  236.     if (INSN_UID (insn) > i)
  237.       i = INSN_UID (insn);
  238.  
  239.   uid_cuid = (short *) alloca ((i + 1) * sizeof (short));
  240.  
  241.   /* Compute the mapping from uids to cuids.
  242.      Cuids are numbers assigned to insns, like uids,
  243.      except that cuids increase monotonically through the code.  */
  244.  
  245.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  246.     INSN_CUID (insn) = ++i;
  247.  
  248.   /* Now scan all the insns in forward order.  */
  249.  
  250.   last_call_cuid = 0;
  251.   mem_last_set = 0;
  252.   prev = 0;
  253.  
  254.   for (insn = f; insn; insn = NEXT_INSN (insn))
  255.     {
  256.       if (GET_CODE (insn) == INSN
  257.       || GET_CODE (insn) == CALL_INSN
  258.       || GET_CODE (insn) == JUMP_INSN)
  259.     {
  260.     retry:
  261.       /* Try this insn with each insn it links back to.  */
  262.  
  263.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  264.         if (try_combine (insn, XEXP (links, 0), 0))
  265.           goto retry;
  266.  
  267.       /* Try each sequence of three linked insns ending with this one.  */
  268.  
  269.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  270.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  271.           for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
  272.            nextlinks = XEXP (nextlinks, 1))
  273.         if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  274.           goto retry;
  275.  
  276.       /* Try to combine a jump insn that uses CC0
  277.          with a preceding insn that sets CC0, and maybe with its
  278.          logical predecessor as well.
  279.          This is how we make decrement-and-branch insns.
  280.          We need this special code because data flow connections
  281.          via CC0 do not get entered in LOG_LINKS.  */
  282.  
  283.       if (GET_CODE (insn) == JUMP_INSN
  284.           && prev != 0
  285.           && GET_CODE (prev) == INSN
  286.           && GET_CODE (PATTERN (prev)) == SET
  287.           && GET_CODE (SET_DEST (PATTERN (prev))) == CC0)
  288.         {
  289.           if (try_combine (insn, prev, 0))
  290.         goto retry;
  291.  
  292.           if (GET_CODE (prev) != NOTE)
  293.         for (nextlinks = LOG_LINKS (prev); nextlinks;
  294.              nextlinks = XEXP (nextlinks, 1))
  295.           if (try_combine (insn, prev, XEXP (nextlinks, 0)))
  296.             goto retry;
  297.         }
  298.  
  299.       /* Try to apply the distributive law to this insn
  300.          and two insns that compute the operands of this one.  */
  301.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  302.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  303.           for (nextlinks = XEXP (links, 1); nextlinks; nextlinks = XEXP (nextlinks, 1))
  304.         if (GET_CODE (XEXP (nextlinks, 0)) != NOTE)
  305.           {
  306.             rtx try_from = 0;
  307.  
  308.             if (GET_CODE (PATTERN (XEXP (links, 0))) == SET
  309.             && find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (links, 0))))
  310.             && GET_CODE (PATTERN (XEXP (nextlinks, 0))) == SET
  311.             && find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (nextlinks, 0)))))
  312.               try_from = try_distrib (insn, XEXP (links, 0), XEXP (nextlinks, 0));
  313.             if (try_from != 0)
  314.               {
  315.             insn = try_from;
  316.             goto retry;
  317.               }
  318.           }
  319. #if 0
  320. /* Turned off because on 68020 it takes four insns to make
  321.    something like (a[b / 32] & (1 << (31 - (b % 32)))) != 0
  322.    that could actually be optimized, and that's an unlikely piece of code.  */
  323.       /* If an insn gets or sets a bit field, try combining it
  324.          with two different insns whose results it uses.  */
  325.       if (GET_CODE (insn) == INSN
  326.           && GET_CODE (PATTERN (insn)) == SET
  327.           && (GET_CODE (SET_DEST (PATTERN (insn))) == ZERO_EXTRACT
  328.           || GET_CODE (SET_DEST (PATTERN (insn))) == SIGN_EXTRACT
  329.           || GET_CODE (SET_SRC (PATTERN (insn))) == ZERO_EXTRACT
  330.           || GET_CODE (SET_SRC (PATTERN (insn))) == SIGN_EXTRACT))
  331.         {
  332.           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  333.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  334.           for (nextlinks = XEXP (links, 1); nextlinks;
  335.                nextlinks = XEXP (nextlinks, 1))
  336.             if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  337.               goto retry;
  338.         }
  339. #endif
  340.       if (GET_CODE (insn) != NOTE)
  341.         record_dead_and_set_regs (insn);
  342.       prev = insn;
  343.     }
  344.       else if (GET_CODE (insn) != NOTE)
  345.     prev = 0;
  346.     }
  347.   total_attempts += combine_attempts;
  348.   total_merges += combine_merges;
  349.   total_extras += combine_extras;
  350.   total_successes += combine_successes;
  351. }
  352.  
  353. /* Try to combine the insns I1 and I2 into I3.
  354.    Here I1 appears earlier than I2, which is earlier than I3.
  355.    I1 can be zero; then we combine just I2 into I3.
  356.  
  357.    Return 1 if successful; if that happens, I1 and I2 are pseudo-deleted
  358.    by turning them into NOTEs, and I3 is modified.
  359.    Return 0 if the combination does not work.  Then nothing is changed.  */
  360.  
  361. static int
  362. try_combine (i3, i2, i1)
  363.      register rtx i3, i2, i1;
  364. {
  365.   register rtx newpat;
  366.   int added_sets_1 = 0;
  367.   int added_sets_2 = 0;
  368.   int total_sets;
  369.   int i2_is_used;
  370.   register rtx link;
  371.   int insn_code_number;
  372.   rtx i2dest, i2src;
  373.   rtx i1dest, i1src;
  374.   int maxreg;
  375.   rtx temp;
  376.   int i;
  377.  
  378.   combine_attempts++;
  379.  
  380.   /* Don't combine with something already used up by combination.  */
  381.  
  382.   if (GET_CODE (i2) == NOTE
  383.       || (i1 && GET_CODE (i1) == NOTE))
  384.     return 0;
  385.  
  386.   /* Don't combine across a CALL_INSN, because that would possibly
  387.      change whether the life span of some REGs crosses calls or not,
  388.      and it is a pain to update that information.  */
  389.  
  390.   if (INSN_CUID (i2) < last_call_cuid
  391.       || (i1 && INSN_CUID (i1) < last_call_cuid))
  392.     return 0;
  393.  
  394.   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  395.      That REG must be either set or dead by the final instruction
  396.      (so that we can safely forget about setting it).
  397.      Also test use_crosses_set_p to make sure that the value
  398.      that is to be substituted for the register
  399.      does not use any registers whose values alter in between.
  400.      Do not try combining with moves from one register to another
  401.      since it is better to let them be tied by register allocation.
  402.      (There is a switch to permit such combination; except the insns
  403.      that copy a function value into another register are never combined
  404.      because moving that too far away from the function call could cause
  405.      something else to be stored in that register in the interim.)
  406.  
  407.      A set of a SUBREG is considered as if it were a set from
  408.      SUBREG.  Thus, (SET (SUBREG:X (REG:Y...)) (something:X...))
  409.      is handled by substituting (SUBREG:Y (something:X...)) for (REG:Y...).  */
  410.  
  411.   if (GET_CODE (PATTERN (i2)) != SET)
  412.     return 0;
  413.   i2dest = SET_DEST (PATTERN (i2));
  414.   i2src = SET_SRC (PATTERN (i2));
  415.   if (GET_CODE (i2dest) == SUBREG)
  416.     {
  417.       i2dest = SUBREG_REG (i2dest);
  418.       i2src = gen_rtx (SUBREG, GET_MODE (i2dest), i2src, 0);
  419.     }
  420.   /* Don't eliminate a store in the stack pointer.  */
  421.   if (i2dest == stack_pointer_rtx)
  422.     return 0;
  423.   if (GET_CODE (i2dest) != CC0
  424.       && (GET_CODE (i2dest) != REG
  425.       || (GET_CODE (i2src) == REG
  426.           && (!flag_combine_regs
  427.           /* Don't substitute a function value reg for any other.  */
  428.           || FUNCTION_VALUE_REGNO_P (REGNO (i2src))))
  429.       || GET_CODE (i2src) == CALL
  430.       /* Don't substitute into an incremented register.  */
  431.       || find_reg_note (i3, REG_INC, i2dest)
  432.       || use_crosses_set_p (i2src, INSN_CUID (i2))))
  433.     return 0;
  434.   /* Don't substitute for a register intended as a clobberable operand.  */
  435.   if (GET_CODE (PATTERN (i3)) == PARALLEL)
  436.     for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
  437.       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
  438.       && XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i2dest)
  439.     return 0;
  440.  
  441.   if (i1 != 0)
  442.     {
  443.       if (GET_CODE (PATTERN (i1)) != SET)
  444.     return 0;
  445.       i1dest = SET_DEST (PATTERN (i1));
  446.       i1src = SET_SRC (PATTERN (i1));
  447.       if (GET_CODE (i1dest) == SUBREG)
  448.     {
  449.       i1dest = SUBREG_REG (i1dest);
  450.       i1src = gen_rtx (SUBREG, GET_MODE (i1dest), i1src, 0);
  451.     }
  452.       if (i1dest == stack_pointer_rtx)
  453.     return 0;
  454.       if (GET_CODE (i1dest) != CC0
  455.       && (GET_CODE (i1dest) != REG
  456.           || (GET_CODE (i1src) == REG
  457.           && (!flag_combine_regs
  458.               || FUNCTION_VALUE_REGNO_P (REGNO (i1src))))
  459.           || GET_CODE (i1src) == CALL
  460.           || find_reg_note (i3, REG_INC, i1dest)
  461.           || find_reg_note (i2, REG_INC, i1dest)
  462.           || use_crosses_set_p (i1src, INSN_CUID (i1))))
  463.     return 0;
  464.       /* Don't substitute for a register intended as a clobberable operand.  */
  465.       if (GET_CODE (PATTERN (i3)) == PARALLEL)
  466.     for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
  467.       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
  468.           && XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i1dest)
  469.         return 0;
  470.     }
  471.  
  472.   /* If it is better that two different modes keep two different pseudos,
  473.      avoid combining them.  */
  474.   if (GET_CODE (PATTERN (i3)) == SET)
  475.     {
  476.       rtx i3dest = SET_DEST (PATTERN (i3));
  477.       while (GET_CODE (i3dest) == SUBREG
  478.          || GET_CODE (i3dest) == STRICT_LOW_PART
  479.          || GET_CODE (i3dest) == SIGN_EXTRACT
  480.          || GET_CODE (i3dest) == ZERO_EXTRACT)
  481.     i3dest = SUBREG_REG (i3dest);
  482.  
  483.       if (SET_SRC (PATTERN (i3)) == i2dest
  484.       && GET_CODE (i3dest) == REG
  485.       && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (i3dest)))
  486.     return 0;
  487.     }
  488.  
  489.   /* If I2 contains anything volatile, reject, unless nothing
  490.      volatile comes between it and I3.  */
  491.   if (volatile_refs_p (PATTERN (i2)))
  492.     {
  493.       rtx insn;
  494.       for (insn = NEXT_INSN (i2); insn != i3; insn = NEXT_INSN (insn))
  495.     if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  496.         || GET_CODE (insn) == JUMP_INSN)
  497.       if (volatile_refs_p (PATTERN (insn)))
  498.         return 0;
  499.     }
  500.   /* Likewise for I1; nothing volatile can come between it and I3,
  501.      except optionally I2.  */
  502.   if (i1 && volatile_refs_p (PATTERN (i1)))
  503.     {
  504.       rtx insn;
  505.       rtx end = (volatile_refs_p (PATTERN (i2)) ? i2 : i3);
  506.       for (insn = NEXT_INSN (i1); insn != end; insn = NEXT_INSN (insn))
  507.     if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  508.         || GET_CODE (insn) == JUMP_INSN)
  509.       if (volatile_refs_p (PATTERN (insn)))
  510.         return 0;
  511.     }
  512.  
  513.   /* If I1 or I2 contains an autoincrement or autodecrement,
  514.      make sure that register is not used between there and I3,
  515.      and not already used in I3 either.
  516.      Also insist that I3 not be a jump; if it were one
  517.      and the incremented register were spilled, we would lose.  */
  518.   for (link = REG_NOTES (i2); link; link = XEXP (link, 1))
  519.     if (REG_NOTE_KIND (link) == REG_INC
  520.     && (GET_CODE (i3) == JUMP_INSN
  521.         || reg_used_between_p (XEXP (link, 0), i2, i3)
  522.         || reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  523.       return 0;
  524.  
  525.   if (i1)
  526.     for (link = REG_NOTES (i1); link; link = XEXP (link, 1))
  527.       if (REG_NOTE_KIND (link) == REG_INC
  528.       && (GET_CODE (i3) == JUMP_INSN
  529.           || reg_used_between_p (XEXP (link, 0), i1, i3)
  530.           || reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  531.     return 0;
  532.  
  533.   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd,
  534.      EXCEPT in one case: I3 has a post-inc in an output operand.  */
  535.   if (!(GET_CODE (PATTERN (i3)) == SET
  536.     && GET_CODE (SET_SRC (PATTERN (i3))) == REG
  537.     && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
  538.     && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
  539.         || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
  540.     /* It's not the exception.  */
  541.     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
  542.       if (REG_NOTE_KIND (link) == REG_INC
  543.       && (reg_mentioned_p (XEXP (link, 0), PATTERN (i2))
  544.           || (i1 != 0
  545.           && reg_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
  546.     return 0;
  547.  
  548.   /* Don't combine an insn I1 or I2 that follows a CC0-setting insn.
  549.      An insn that uses CC0 must not be separated from the one that sets it.
  550.      It would be more logical to test whether CC0 occurs inside I1 or I2,
  551.      but that would be much slower, and this ought to be equivalent.  */
  552.   temp = PREV_INSN (i2);
  553.   while (temp && GET_CODE (temp) == NOTE)
  554.     temp = PREV_INSN (temp);
  555.   if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
  556.     return 0;
  557.   if (i1)
  558.     {
  559.       temp = PREV_INSN (i2);
  560.       while (temp && GET_CODE (temp) == NOTE)
  561.     temp = PREV_INSN (temp);
  562.       if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
  563.     return 0;
  564.     }
  565.  
  566.   /* See if the SETs in i1 or i2 need to be kept around in the merged
  567.      instruction: whenever the value set there is still needed past i3.  */
  568.   added_sets_2 = (GET_CODE (i2dest) != CC0
  569.           && ! dead_or_set_p (i3, i2dest));
  570.   if (i1)
  571.     added_sets_1 = ! (dead_or_set_p (i3, i1dest)
  572.               || dead_or_set_p (i2, i1dest));
  573.  
  574.   combine_merges++;
  575.  
  576.   undobuf.num_undo = 0;
  577.   undobuf.storage = 0;
  578.  
  579.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  580.  
  581.   maxreg = max_reg_num ();
  582.  
  583.   subst_insn = i3;
  584.   n_occurrences = 0;        /* `subst' counts here */
  585.  
  586.   newpat = subst (PATTERN (i3), i2dest, i2src);
  587.   /* Record whether i2's body now appears within i3's body.  */
  588.   i2_is_used = n_occurrences;
  589.  
  590.   if (i1)
  591.     {
  592.       n_occurrences = 0;
  593.       newpat = subst (newpat, i1dest, i1src);
  594.     }
  595.  
  596.   if (GET_CODE (PATTERN (i3)) == SET
  597.       && SET_DEST (PATTERN (i3)) == cc0_rtx
  598.       && (GET_CODE (SET_SRC (PATTERN (i3))) == AND
  599.       || GET_CODE (SET_SRC (PATTERN (i3))) == LSHIFTRT)
  600.       && next_insn_tests_no_inequality (i3))
  601.     simplify_set_cc0_and (i3);
  602.  
  603.   if (max_reg_num () != maxreg)
  604.     abort ();
  605.  
  606.   /* If the actions of the earler insns must be kept
  607.      in addition to substituting them into the latest one,
  608.      we must make a new PARALLEL for the latest insn
  609.      to hold additional the SETs.  */
  610.  
  611.   if (added_sets_1 || added_sets_2)
  612.     {
  613.       combine_extras++;
  614.  
  615.       /* Arrange to free later what we allocate now
  616.      if we don't accept this combination.  */
  617.       if (!undobuf.storage)
  618.     undobuf.storage = (char *) oballoc (0);
  619.  
  620.       if (GET_CODE (newpat) == PARALLEL)
  621.     {
  622.       rtvec old = XVEC (newpat, 0);
  623.       total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
  624.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  625.       bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
  626.          sizeof (old->elem[0]) * old->num_elem);
  627.     }
  628.       else
  629.     {
  630.       rtx old = newpat;
  631.       total_sets = 1 + added_sets_1 + added_sets_2;
  632.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  633.       XVECEXP (newpat, 0, 0) = old;
  634.     }
  635.      if (added_sets_1)
  636.     {
  637.       XVECEXP (newpat, 0, --total_sets) = PATTERN (i1);
  638.     }
  639.      if (added_sets_2)
  640.     {
  641.       /* If there is no I1, use I2's body as is.  */
  642.       if (i1 == 0
  643.       /* If I2 was stuck into I3, then anything within it has
  644.          already had I1 substituted into it when that was done to I3.  */
  645.           || i2_is_used)
  646.         {
  647.           XVECEXP (newpat, 0, --total_sets) = PATTERN (i2);
  648.         }
  649.       else
  650.         XVECEXP (newpat, 0, --total_sets)
  651.           = subst (PATTERN (i2), i1dest, i1src);
  652.     }
  653.     }
  654.  
  655.   /* Fail if an autoincrement side-effect has been duplicated.  */
  656.   if ((i2_is_used > 1 && find_reg_note (i2, REG_INC, 0) != 0)
  657.       || (i1 != 0 && n_occurrences > 1 && find_reg_note (i1, REG_INC, 0) != 0))
  658.     {
  659.       undo_all ();
  660.       return 0;
  661.     }
  662.  
  663.   /* Is the result of combination a valid instruction?  */
  664.   insn_code_number = recog (newpat, i3);
  665.  
  666.   if (insn_code_number >= 0
  667.       /* Is the result a reasonable ASM_OPERANDS?  */
  668.       || (check_asm_operands (newpat) && ! added_sets_1 && ! added_sets_2))
  669.     {
  670.       /* Yes.  Install it.  */
  671.       register int regno;
  672.       INSN_CODE (i3) = insn_code_number;
  673.       PATTERN (i3) = newpat;
  674.       /* If anything was substituted more than once,
  675.      copy it to avoid invalid shared rtl structure.  */
  676.       copy_substitutions ();
  677.       /* The data flowing into I2 now flows into I3.
  678.      But we cannot always move all of I2's LOG_LINKS into I3,
  679.      since they must go to a setting of a REG from the
  680.      first use following.  If I2 was the first use following a set,
  681.      I3 is now a use, but it is not the first use
  682.      if some instruction between I2 and I3 is also a use.
  683.      Here, for simplicity, we move all the links only if
  684.      there are no real insns between I2 and I3.
  685.      Otherwise, we move only links that correspond to regs
  686.      that used to die in I2.  They are always safe to move.  */
  687.       add_links (i3, i2, adjacent_insns_p (i2, i3));
  688.       /* Most REGs that previously died in I2 now die in I3.  */ 
  689.       move_deaths (i2src, INSN_CUID (i2), i3);
  690.       if (GET_CODE (i2dest) == REG)
  691.     {
  692.       /* If the reg formerly set in I2 died only once and that was in I3,
  693.          zero its use count so it won't make `reload' do any work.  */
  694.       regno = REGNO (i2dest);
  695.       if (! added_sets_2)
  696.         {
  697.           reg_n_sets[regno]--;
  698.           /* Used to check  && regno_dead_p (regno, i3)  also here.  */
  699.           if (reg_n_sets[regno] == 0
  700.           && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  701.             & (1 << (regno % HOST_BITS_PER_INT))))
  702.         reg_n_refs[regno] = 0;
  703.         }
  704.       /* If a ref to REGNO was substituted into I3 from I2,
  705.          then it still dies there if it previously did.
  706.          Otherwise either REGNO never did die in I3 so remove_death is safe
  707.          or this entire life of REGNO is gone so remove its death.  */
  708.       if (!added_sets_2
  709.           && ! reg_mentioned_p (i2dest, PATTERN (i3)))
  710.         remove_death (regno, i3);
  711.     }
  712.       /* Any registers previously autoincremented in I2
  713.      are now incremented in I3.  */
  714.       add_incs (i3, REG_NOTES (i2));
  715.       if (i1)
  716.     {
  717.       /* Likewise, merge the info from I1 and get rid of it.  */
  718.       add_links (i3, i1,
  719.              adjacent_insns_p (i1, i2) && adjacent_insns_p (i2, i3));
  720.       move_deaths (i1src, INSN_CUID (i1), i3);
  721.       if (GET_CODE (i1dest) == REG)
  722.         {
  723.           regno = REGNO (i1dest);
  724.           if (! added_sets_1)
  725.         {
  726.           reg_n_sets[regno]--;
  727.           /* Used to also check  && regno_dead_p (regno, i3) here.  */
  728.  
  729.           if (reg_n_sets[regno] == 0
  730.               && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  731.                 & (1 << (regno % HOST_BITS_PER_INT))))
  732.  
  733.             reg_n_refs[regno] = 0;
  734.         }
  735.           /* If a ref to REGNO was substituted into I3 from I1,
  736.          then it still dies there if it previously did.
  737.          Else either REGNO never did die in I3 so remove_death is safe
  738.          or this entire life of REGNO is gone so remove its death.  */
  739.           if (! added_sets_1
  740.           && ! reg_mentioned_p (i1dest, PATTERN (i3)))
  741.         remove_death (regno, i3);
  742.         }
  743.       add_incs (i3, REG_NOTES (i1));
  744.       LOG_LINKS (i1) = 0;
  745.       PUT_CODE (i1, NOTE);
  746.       NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
  747.       NOTE_SOURCE_FILE (i1) = 0;
  748.     }
  749.       /* Get rid of I2.  */
  750.       LOG_LINKS (i2) = 0;
  751.       PUT_CODE (i2, NOTE);
  752.       NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
  753.       NOTE_SOURCE_FILE (i2) = 0;
  754.  
  755.       combine_successes++;
  756.       return 1;
  757.     }
  758.  
  759.   /* Failure: change I3 back the way it was.  */
  760.   undo_all ();
  761.  
  762.   return 0;
  763. }
  764.  
  765. /* Undo all the modifications recorded in undobuf.  */
  766.  
  767. static void
  768. undo_all ()
  769. {
  770.   register int i;
  771.   if (undobuf.num_undo > MAX_UNDO)
  772.     undobuf.num_undo = MAX_UNDO;
  773.   for (i = undobuf.num_undo - 1; i >= 0; i--)
  774.     *undobuf.undo[i].where = undobuf.undo[i].old_contents;
  775.   if (undobuf.storage)
  776.     obfree (undobuf.storage);
  777.   undobuf.num_undo = 0;
  778.   undobuf.storage = 0;
  779. }
  780.  
  781. /* If this insn had more than one substitution,
  782.    copy all but one, so that no invalid shared substructure is introduced.  */
  783.  
  784. static void
  785. copy_substitutions ()
  786. {
  787.   register int i;
  788.   if (undobuf.num_undo > 1)
  789.     {
  790.       for (i = undobuf.num_undo - 1; i >= 1; i--)
  791.     if (! undobuf.undo[i].is_int)
  792.       *undobuf.undo[i].where = copy_rtx (*undobuf.undo[i].where);
  793.     }
  794. }
  795.  
  796. /* Throughout X, replace FROM with TO, and return the result.
  797.    The result is TO if X is FROM;
  798.    otherwise the result is X, but its contents may have been modified.
  799.    If they were modified, a record was made in undobuf so that
  800.    undo_all will (among other things) return X to its original state.
  801.  
  802.    If the number of changes necessary is too much to record to undo,
  803.    the excess changes are not made, so the result is invalid.
  804.    The changes already made can still be undone.
  805.    undobuf.num_undo is incremented for such changes, so by testing that
  806.    the caller can tell whether the result is valid.
  807.  
  808.    `n_occurrences' is incremented each time FROM is replaced.  */
  809.  
  810. static rtx
  811. subst (x, from, to)
  812.      register rtx x, from, to;
  813. {
  814.   register char *fmt;
  815.   register int len, i;
  816.   register enum rtx_code code;
  817. #if (defined(atarist) || defined(atariminix))
  818.   short was_replaced[2];    /* 'char' confuses GAS 1.14... */
  819. #else
  820.   char was_replaced[2];
  821. #endif
  822.  
  823. #define SUBST(INTO, NEWVAL)  \
  824.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  825.     {                                \
  826.       undobuf.undo[undobuf.num_undo].where = &INTO;            \
  827.       undobuf.undo[undobuf.num_undo].old_contents = INTO;        \
  828.       undobuf.undo[undobuf.num_undo].is_int = 0;            \
  829.       INTO = NEWVAL;                        \
  830.     }                                \
  831.       undobuf.num_undo++; } while (0)
  832.  
  833. #define SUBST_INT(INTO, NEWVAL)  \
  834.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  835.     {                                \
  836.       struct undo_int *u = (struct undo_int *)&undobuf.undo[undobuf.num_undo];\
  837.       u->where = &INTO;                        \
  838.       u->old_contents = INTO;                    \
  839.       u->is_int = 1;                        \
  840.       INTO = NEWVAL;                        \
  841.     }                                \
  842.       undobuf.num_undo++; } while (0)
  843.  
  844. /* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
  845.    replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
  846.    If it is 0, that cannot be done.  We can now do this for any MEM
  847.    because (SUBREG (MEM...)) is guaranteed to cause the MEM to be reloaded.
  848.    If not for that, MEM's would very rarely be safe.  */
  849.  
  850. /* Reject MODEs bigger than a word, because we might not be able
  851.    to reference a two-register group starting with an arbitrary register
  852.    (and currently gen_lowpart might crash for a SUBREG).  */
  853.  
  854. #define FAKE_EXTEND_SAFE_P(MODE, FROM) \
  855.   (GET_MODE_SIZE (MODE) <= UNITS_PER_WORD            \
  856.    && (GET_CODE (FROM) == REG || GET_CODE (FROM) == SUBREG    \
  857.        || GET_CODE (FROM) == MEM))
  858.  
  859.   if (x == from)
  860.     return to;
  861.  
  862.   /* It is possible to have a subexpression appear twice in the insn.
  863.      Suppose that FROM is a register that appears within TO.
  864.      Then, after that subexpression has been scanned once by `subst',
  865.      the second time it is scanned, TO may be found.  If we were
  866.      to scan TO here, we would find FROM within it and create a
  867.      self-referent rtl structure which is completely wrong.  */
  868.   if (x == to)
  869.     return to;
  870.  
  871.   code = GET_CODE (x);
  872.  
  873.   /* A little bit of algebraic simplification here.  */
  874.   switch (code)
  875.     {
  876.       /* This case has no effect except to speed things up.  */
  877.     case REG:
  878.     case CONST_INT:
  879.     case CONST:
  880.     case SYMBOL_REF:
  881.     case LABEL_REF:
  882.     case PC:
  883.     case CC0:
  884.       return x;
  885.     }
  886.  
  887.   was_replaced[0] = 0;
  888.   was_replaced[1] = 0;
  889.  
  890.   len = GET_RTX_LENGTH (code);
  891.   fmt = GET_RTX_FORMAT (code);
  892.  
  893.   /* Don't replace FROM where it is being stored in rather than used.  */
  894.   if (code == SET && SET_DEST (x) == from)
  895.     fmt = "ie";
  896.   if (code == SET && GET_CODE (SET_DEST (x)) == SUBREG
  897.       && SUBREG_REG (SET_DEST (x)) == from)
  898.     fmt = "ie";
  899.  
  900.   for (i = 0; i < len; i++)
  901.     {
  902.       if (fmt[i] == 'E')
  903.     {
  904.       register int j;
  905.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  906.         {
  907.           register rtx new;
  908.           if (XVECEXP (x, i, j) == from)
  909.         new = to, n_occurrences++;
  910.           else
  911.         new = subst (XVECEXP (x, i, j), from, to);
  912.           if (new != XVECEXP (x, i, j))
  913.         SUBST (XVECEXP (x, i, j), new);
  914.         }
  915.     }
  916.       else if (fmt[i] == 'e')
  917.     {
  918.       register rtx new;
  919.  
  920.       if (XEXP (x, i) == from)
  921.         {
  922.           new = to;
  923.           n_occurrences++;
  924.           if (i < 2)
  925.         was_replaced[i] = 1;
  926.         }
  927.       else
  928.         new = subst (XEXP (x, i), from, to);
  929.  
  930.       if (new != XEXP (x, i))
  931.         SUBST (XEXP (x, i), new);
  932.     }
  933.     }
  934.  
  935.   /* A little bit of algebraic simplification here.  */
  936.   switch (code)
  937.     {
  938.     case SUBREG:
  939.       /* Changing mode twice with SUBREG => just change it once,
  940.      or not at all if changing back to starting mode.  */
  941.       if (SUBREG_REG (x) == to
  942.       && GET_CODE (to) == SUBREG)
  943.     {
  944.       if (GET_MODE (x) == GET_MODE (SUBREG_REG (to)))
  945.         if (SUBREG_WORD (x) == 0 && SUBREG_WORD (to) == 0)
  946.           return SUBREG_REG (to);
  947.       SUBST (SUBREG_REG (x), SUBREG_REG (to));
  948.       if (SUBREG_WORD (to) != 0)
  949.         SUBST_INT (SUBREG_WORD (x), SUBREG_WORD (x) + SUBREG_WORD (to));
  950.     }
  951.       if (SUBREG_REG (x) == to
  952.       && (GET_CODE (to) == SIGN_EXTEND || GET_CODE (to) == ZERO_EXTEND)
  953.       && subreg_lowpart_p (x))
  954.     {
  955.       /* (subreg (sign_extend X)) is X, if it has same mode as X.  */
  956.       if (GET_MODE (x) == GET_MODE (XEXP (to, 0)))
  957.         return XEXP (to, 0);
  958.       /* (subreg (sign_extend X)), if it has a mode wider than X,
  959.          can be done with (sign_extend X).  */
  960.       if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
  961.         {
  962.           if (!undobuf.storage)
  963.         undobuf.storage = (char *) oballoc (0);
  964.           return gen_rtx (GET_CODE (to), GET_MODE (x), XEXP (to, 0));
  965.         }
  966.       /* Extend and then truncate smaller than it was to start with:
  967.          no need to extend.  */
  968.       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
  969.         {
  970.           SUBST (XEXP (x, 0), XEXP (to, 0));
  971.         }
  972.     }
  973.       /* (subreg:A (mem:B X) N) becomes a modified MEM.
  974.      If we can't do that safely, then it becomes something nonsensical
  975.      so that this combination won't take place.
  976.      This avoids producing any (subreg (mem))s except in the special
  977.      paradoxical case where gen_lowpart_for_combine makes them.  */
  978.       if (SUBREG_REG (x) == to
  979.       && GET_CODE (to) == MEM)
  980.     {
  981.       int endian_offset = 0;
  982.       /* Don't combine this if mode A is wider than B.  */
  983.       if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (to)))
  984.         return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  985.       /* Don't change the mode of the MEM
  986.          if that would change the meaning of the address.  */
  987.       if (mode_dependent_address_p (XEXP (to, 0)))
  988.         return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  989. #ifdef BYTES_BIG_ENDIAN
  990.       if (GET_MODE_SIZE (GET_MODE (x)) < UNITS_PER_WORD)
  991.         endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (x));
  992.       if (GET_MODE_SIZE (GET_MODE (to)) < UNITS_PER_WORD)
  993.         endian_offset -= UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (to));
  994. #endif
  995.       if (!undobuf.storage)
  996.         undobuf.storage = (char *) oballoc (0);
  997.       /* Note if the plus_constant doesn't make a valid address
  998.          then this combination won't be accepted.  */
  999.       return gen_rtx (MEM, GET_MODE (x),
  1000.               plus_constant (XEXP (to, 0),
  1001.                      (SUBREG_WORD (x) * UNITS_PER_WORD
  1002.                       + endian_offset)));
  1003.     }
  1004.       break;
  1005.  
  1006.     case NOT:
  1007.       /* (not (minus X 1)) can become (neg X).  */
  1008.       if (was_replaced[0]
  1009.       && ((GET_CODE (to) == PLUS && INTVAL (XEXP (to, 1)) == -1)
  1010.           || (GET_CODE (to) == MINUS && XEXP (to, 1) == const1_rtx)))
  1011.     {
  1012.       if (!undobuf.storage)
  1013.         undobuf.storage = (char *) oballoc (0);
  1014.       return gen_rtx (NEG, GET_MODE (to), XEXP (to, 0));
  1015.     }
  1016.       /* Don't let substitution introduce double-negatives.  */
  1017.       if (was_replaced[0]
  1018.       && GET_CODE (to) == code)
  1019.     return XEXP (to, 0);
  1020.       break;
  1021.  
  1022.     case NEG:
  1023.       /* (neg (minus X Y)) can become (minus Y X).  */
  1024.       if (was_replaced[0] && GET_CODE (to) == MINUS)
  1025.     {
  1026.       if (!undobuf.storage)
  1027.         undobuf.storage = (char *) oballoc (0);
  1028.       return gen_rtx (MINUS, GET_MODE (to),
  1029.               XEXP (to, 1), XEXP (to, 0));
  1030.     }
  1031.       /* Don't let substitution introduce double-negatives.  */
  1032.       if (was_replaced[0]
  1033.       && GET_CODE (to) == code)
  1034.     return XEXP (to, 0);
  1035.       break;
  1036.  
  1037.     case FLOAT_TRUNCATE:
  1038.       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
  1039.       if (was_replaced[0]
  1040.       && GET_CODE (to) == FLOAT_EXTEND
  1041.       && GET_MODE (XEXP (to, 0)) == GET_MODE (x))
  1042.     return XEXP (to, 0);
  1043.       break;
  1044.  
  1045. #if 0
  1046.     case COMPARE:
  1047.       /* -x>0 if 0>x.  */
  1048.       if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
  1049.     {
  1050.       SUBST (XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  1051.       SUBST (XEXP (x, 0), const0_rtx);
  1052.     }
  1053.       if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
  1054.     {
  1055.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  1056.       SUBST (XEXP (x, 1), const0_rtx);
  1057.     }
  1058.       break;
  1059. #endif
  1060.  
  1061.     case PLUS:
  1062. #if 0  /* Turned off for caution: turn it on after 1.36.  */
  1063.       /* Identify constant sums as such.  */
  1064.       if ((was_replaced[0] || was_replaced[1])
  1065.       && CONSTANT_P (XEXP (x, 0))
  1066.       && CONSTANT_P (XEXP (x, 1)))
  1067.     {
  1068.       if (!undobuf.storage)
  1069.         undobuf.storage = (char *) oballoc (0);
  1070.       return gen_rtx (CONST, GET_MODE (x), x);
  1071.     }
  1072. #endif
  1073.       /* In (plus <foo> (ashift <bar> <n>))
  1074.      change the shift to a multiply so we can recognize
  1075.      scaled indexed addresses.  */
  1076.       if ((was_replaced[0]
  1077.        || was_replaced[1])
  1078.       && GET_CODE (to) == ASHIFT
  1079.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1080.       && INTVAL (XEXP (to, 1)) < HOST_BITS_PER_INT)
  1081.     {
  1082.       rtx temp;
  1083.       if (!undobuf.storage)
  1084.         undobuf.storage = (char *) oballoc (0);
  1085.       temp = gen_rtx (MULT, GET_MODE (to),
  1086.               XEXP (to, 0),
  1087.               gen_rtx (CONST_INT, VOIDmode,
  1088.                    1 << INTVAL (XEXP (to, 1))));
  1089.       if (was_replaced[0])
  1090.         SUBST (XEXP (x, 0), temp);
  1091.       else
  1092.         SUBST (XEXP (x, 1), temp);
  1093.     }
  1094.       /* (plus X (neg Y)) becomes (minus X Y).  */
  1095.       if (GET_CODE (XEXP (x, 1)) == NEG)
  1096.     {
  1097.       if (!undobuf.storage)
  1098.         undobuf.storage = (char *) oballoc (0);
  1099.       return gen_rtx (MINUS, GET_MODE (x),
  1100.               XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  1101.     }
  1102.       /* (plus (neg X) Y) becomes (minus Y X).  */
  1103.       if (GET_CODE (XEXP (x, 0)) == NEG)
  1104.     {
  1105.       if (!undobuf.storage)
  1106.         undobuf.storage = (char *) oballoc (0);
  1107.       return gen_rtx (MINUS, GET_MODE (x),
  1108.               XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  1109.     }
  1110.       /* (plus (plus x c1) c2) => (plus x c1+c2) */
  1111.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1112.       && GET_CODE (XEXP (x, 0)) == PLUS
  1113.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  1114.     {
  1115.       int sum = (INTVAL (XEXP (x, 1))
  1116.              + INTVAL (XEXP (XEXP (x, 0), 1)));
  1117.       if (sum == 0)
  1118.         return XEXP (XEXP (x, 0), 0);
  1119.       if (!undobuf.storage)
  1120.         undobuf.storage = (char *) oballoc (0);
  1121.       SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, sum));
  1122.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1123.       break;
  1124.     }
  1125.       /* If we have something (putative index) being added to a sum,
  1126.      associate it so that any constant term is outermost.
  1127.      That's because that's the way indexed addresses are
  1128.      now supposed to appear.  */
  1129.       if (((was_replaced[0] && GET_CODE (XEXP (x, 1)) == PLUS)
  1130.        || (was_replaced[1] && GET_CODE (XEXP (x, 0)) == PLUS))
  1131.       ||
  1132.       ((was_replaced[0] || was_replaced[1])
  1133.        && GET_CODE (to) == PLUS))
  1134.     {
  1135.       rtx offset = 0, base, index;
  1136.       if (GET_CODE (to) != PLUS)
  1137.         {
  1138.           index = to;
  1139.           base = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  1140.         }
  1141.       else
  1142.         {
  1143.           index = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  1144.           base = to;
  1145.         }
  1146.       if (CONSTANT_ADDRESS_P (XEXP (base, 0)))
  1147.         {
  1148.           offset = XEXP (base, 0);
  1149.           base = XEXP (base, 1);
  1150.         }
  1151.       else if (CONSTANT_ADDRESS_P (XEXP (base, 1)))
  1152.         {
  1153.           offset = XEXP (base, 1);
  1154.           base = XEXP (base, 0);
  1155.         }
  1156.       if (offset != 0)
  1157.         {
  1158.           if (!undobuf.storage)
  1159.         undobuf.storage = (char *) oballoc (0);
  1160.           if (GET_CODE (offset) == CONST_INT)
  1161.         return plus_constant (gen_rtx (PLUS, GET_MODE (index),
  1162.                            base, index),
  1163.                       INTVAL (offset));
  1164.           if (GET_CODE (index) == CONST_INT)
  1165.         return plus_constant (gen_rtx (PLUS, GET_MODE (offset),
  1166.                            base, offset),
  1167.                       INTVAL (index));
  1168.           return gen_rtx (PLUS, GET_MODE (index),
  1169.                   gen_rtx (PLUS, GET_MODE (index),
  1170.                        base, index),
  1171.                   offset);
  1172.         }
  1173.     }
  1174.       break;
  1175.  
  1176.     case EQ:
  1177.     case NE:
  1178.       /* If comparing a subreg against zero, discard the subreg.  */
  1179.       if (was_replaced[0]
  1180.       && GET_CODE (to) == SUBREG
  1181.       && SUBREG_WORD (to) == 0
  1182.       && XEXP (x, 1) == const0_rtx)
  1183.     SUBST (XEXP (x, 0), SUBREG_REG (to));
  1184.  
  1185.       /* If comparing a ZERO_EXTRACT against zero,
  1186.      canonicalize to a SIGN_EXTRACT,
  1187.      since the two are equivalent here.  */
  1188.       if (was_replaced[0]
  1189.       && GET_CODE (to) == ZERO_EXTRACT
  1190.       && XEXP (x, 1) == const0_rtx)
  1191.     {
  1192.       if (!undobuf.storage)
  1193.         undobuf.storage = (char *) oballoc (0);
  1194.       SUBST (XEXP (x, 0),
  1195.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  1196.               XEXP (to, 0), XEXP (to, 1),
  1197.               XEXP (to, 2)));
  1198.     }
  1199.       /* If we are putting (ASHIFT 1 x) into (EQ (AND ... y) 0),
  1200.      arrange to return (EQ (SIGN_EXTRACT y 1 x) 0),
  1201.      which is what jump-on-bit instructions are written with.  */
  1202.       else if (XEXP (x, 1) == const0_rtx
  1203.            && GET_CODE (XEXP (x, 0)) == AND
  1204.            && (XEXP (XEXP (x, 0), 0) == to
  1205.            || XEXP (XEXP (x, 0), 1) == to)
  1206.            && GET_CODE (to) == ASHIFT
  1207.            && XEXP (to, 0) == const1_rtx)
  1208.     {
  1209.       register rtx y = XEXP (XEXP (x, 0),
  1210.                  XEXP (XEXP (x, 0), 0) == to);
  1211.       if (!undobuf.storage)
  1212.         undobuf.storage = (char *) oballoc (0);
  1213.       SUBST (XEXP (x, 0),
  1214.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  1215.               y,
  1216.               const1_rtx, XEXP (to, 1)));
  1217.     }
  1218.       /* Negation is a no-op before equality test against zero.  */
  1219.       if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
  1220.     {
  1221.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1222.     }
  1223.       if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
  1224.     {
  1225.       SUBST (XEXP (x, 1), XEXP (XEXP (x, 1), 0));
  1226.     }
  1227.       break;
  1228.  
  1229.     case ZERO_EXTEND:
  1230.       /* Nested zero-extends are equivalent to just one.  */
  1231.       if (was_replaced[0]
  1232.       && GET_CODE (to) == ZERO_EXTEND)
  1233.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1234.       /* Zero extending a constant int can be replaced
  1235.      by a zero-extended constant.  */
  1236.       if (was_replaced[0]
  1237.       && HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
  1238.       && GET_CODE (to) == CONST_INT)
  1239.     {
  1240.       int intval = INTVAL (to) & GET_MODE_MASK (GET_MODE (from));
  1241.       if (!undobuf.storage)
  1242.         undobuf.storage = (char *) oballoc (0);
  1243.       return gen_rtx (CONST_INT, VOIDmode, intval);
  1244.     }
  1245.       /* Zero-extending the result of an and with a constant can be done
  1246.      with a wider and.  */
  1247.       if (was_replaced[0]
  1248.       && GET_CODE (to) == AND
  1249.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1250.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1251.       /* Avoid getting wrong result if the constant has high bits set
  1252.          that are irrelevant in the narrow mode where it is being used.  */
  1253.       && 0 == (INTVAL (XEXP (to, 1))
  1254.            & ~ GET_MODE_MASK (GET_MODE (to))))
  1255.     {
  1256.       if (!undobuf.storage)
  1257.         undobuf.storage = (char *) oballoc (0);
  1258.       return gen_rtx (AND, GET_MODE (x),
  1259.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1260.               XEXP (to, 1));
  1261.     } 
  1262.       /* Change (zero_extend:M (subreg:N (zero_extract:M ...) 0))
  1263.      to (zero_extract:M ...) if the field extracted fits in mode N.  */
  1264.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1265.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT
  1266.       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  1267.       && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  1268.           <= GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
  1269.     {
  1270.       return XEXP (XEXP (x, 0), 0);
  1271.     }
  1272.       /* Change (zero_extend:M (subreg:N (and:M ... <const>) 0))
  1273.      to (and:M ...) if the significant bits fit in mode N.  */
  1274.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1275.       && SUBREG_REG (XEXP (x, 0)) == to
  1276.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1277.       && GET_CODE (to) == AND
  1278.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1279.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1280.       /* Avoid getting wrong result if the constant has high bits set
  1281.          that are irrelevant in the narrow mode where it is being used.  */
  1282.       && 0 == (INTVAL (XEXP (to, 1))
  1283.            & ~ GET_MODE_MASK (GET_MODE (to))))
  1284.     {
  1285.       if (!undobuf.storage)
  1286.         undobuf.storage = (char *) oballoc (0);
  1287.       return gen_rtx (AND, GET_MODE (x),
  1288.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1289.               XEXP (to, 1));
  1290.     }
  1291.       /* In (zero_extend:M (subreg:N (lshiftrt:M REG))),
  1292.      where REG was assigned from (zero_extend:M (any:N ...)),
  1293.      remove the outer zero extension.  */
  1294.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1295.       && SUBREG_REG (XEXP (x, 0)) == to
  1296.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1297.       && GET_CODE (to) == LSHIFTRT)
  1298.     {
  1299.       rtx tmp = XEXP (to, 0);
  1300.  
  1301.       /* See if arg of LSHIFTRT is a register whose value we can find.  */
  1302.       if (GET_CODE (tmp) == REG)
  1303.         if (reg_n_sets[REGNO (tmp)] == 1
  1304.         && SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
  1305.           tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
  1306.         else
  1307.           break;
  1308.  
  1309.       if (GET_CODE (tmp) == ZERO_EXTEND
  1310.           && GET_MODE (tmp) == GET_MODE (x)
  1311.           && GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
  1312.         return SUBREG_REG (XEXP (x, 0));
  1313.     }
  1314.       break;
  1315.  
  1316.     case SIGN_EXTEND:
  1317.       /* Nested sign-extends are equivalent to just one.  */
  1318.       if (was_replaced[0]
  1319.       && GET_CODE (to) == SIGN_EXTEND)
  1320.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1321.       /* Sign extending a constant int can be replaced
  1322.      by a sign-extended constant.  */
  1323.       if (was_replaced[0]
  1324.       && HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
  1325.       && GET_CODE (to) == CONST_INT)
  1326.     {
  1327.       int intval = INTVAL (to);
  1328.       if (!undobuf.storage)
  1329.         undobuf.storage = (char *) oballoc (0);
  1330.       if (intval > 0
  1331.           && (intval & (1 << (GET_MODE_BITSIZE (GET_MODE (from)) - 1))))
  1332.         intval |= ~ GET_MODE_MASK (GET_MODE (from));
  1333.       return gen_rtx (CONST_INT, VOIDmode, intval);
  1334.     }
  1335.       /* Sign-extending the result of an and with a constant can be done
  1336.      with a wider and, provided the high bit of the constant is 0.  */
  1337.       if (was_replaced[0]
  1338.       && GET_CODE (to) == AND
  1339.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1340.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1341.       && ((INTVAL (XEXP (to, 1))
  1342.            & (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
  1343.           == 0))
  1344.     {
  1345.       if (!undobuf.storage)
  1346.         undobuf.storage = (char *) oballoc (0);
  1347.       return gen_rtx (AND, GET_MODE (x),
  1348.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1349.               XEXP (to, 1));
  1350.      } 
  1351.       /* hacks added by tiemann.  */
  1352.       /* Change (sign_extend:M (subreg:N (and:M ... <const>) 0))
  1353.      to (and:M ...), provided the result fits in mode N,
  1354.      and the high bit of the constant is 0.  */
  1355.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1356.       && SUBREG_REG (XEXP (x, 0)) == to
  1357.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1358.       && GET_CODE (to) == AND
  1359.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1360.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1361.       && ((INTVAL (XEXP (to, 1))
  1362.            & (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
  1363.           == 0))
  1364.     {
  1365.       if (!undobuf.storage)
  1366.         undobuf.storage = (char *) oballoc (0);
  1367.       return gen_rtx (AND, GET_MODE (x),
  1368.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1369.               XEXP (to, 1));
  1370.     } 
  1371.       /* In (sign_extend:M (subreg:N (ashiftrt:M REG))),
  1372.      where REG was assigned from (sign_extend:M (any:N ...)),
  1373.      remove the outer sign extension.  */
  1374.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1375.       && SUBREG_REG (XEXP (x, 0)) == to
  1376.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1377.       && GET_CODE (to) == ASHIFTRT)
  1378.     {
  1379.       rtx tmp = XEXP (to, 0);
  1380.  
  1381.       /* See if arg of LSHIFTRT is a register whose value we can find.  */
  1382.       if (GET_CODE (tmp) == REG)
  1383.         if (reg_n_sets[REGNO (tmp)] == 1
  1384.         && SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
  1385.           tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
  1386.         else
  1387.           break;
  1388.  
  1389.       if (GET_CODE (tmp) == SIGN_EXTEND
  1390.           && GET_MODE (tmp) == GET_MODE (x)
  1391.           && GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
  1392.         return SUBREG_REG (XEXP (x, 0));
  1393.     }
  1394.       break;
  1395.  
  1396.     case SET:
  1397.       /* In (set (zero-extract <x> <n> <y>) (and <foo> <(2**n-1) | anything>))
  1398.      the `and' can be deleted.  This can happen when storing a bit
  1399.      that came from a set-flag insn followed by masking to one bit.  */
  1400.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1401.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1402.       && was_replaced[1]
  1403.       && GET_CODE (to) == AND
  1404.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1405.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1406.            & ~ INTVAL (XEXP (to, 1))))
  1407.     {
  1408.       SUBST (XEXP (x, 1), XEXP (to, 0));
  1409.     } 
  1410.       /* In (set (zero-extract <x> <n> <y>)
  1411.          (subreg (and <foo> <(2**n-1) | anything>)))
  1412.      the `and' can be deleted.  */
  1413.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1414.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1415.       && GET_CODE (XEXP (x, 1)) == SUBREG
  1416.       && SUBREG_WORD (XEXP (x, 1)) == 0
  1417.       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == AND
  1418.       && GET_CODE (XEXP (SUBREG_REG (XEXP (x, 1)), 1)) == CONST_INT
  1419.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1420.            & ~ INTVAL (XEXP (SUBREG_REG (XEXP (x, 1)), 1))))
  1421.     {
  1422.       SUBST (SUBREG_REG (XEXP (x, 1)), XEXP (SUBREG_REG (XEXP (x, 1)), 0));
  1423.     } 
  1424.       /* (set (zero_extract ...) (and/or/xor (zero_extract ...) const)),
  1425.      if both zero_extracts have the same location, size and position,
  1426.      can be changed to avoid the byte extracts.  */
  1427.       if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1428.        || GET_CODE (XEXP (x, 0)) == SIGN_EXTRACT)
  1429.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1430.       && (GET_CODE (XEXP (x, 1)) == AND
  1431.           || GET_CODE (XEXP (x, 1)) == IOR
  1432.           || GET_CODE (XEXP (x, 1)) == XOR)
  1433.       && rtx_equal_p (XEXP (x, 0), XEXP (XEXP (x, 1), 0))
  1434.       && GET_CODE (XEXP (XEXP (x, 1), 0)) == GET_CODE (XEXP (x, 0))
  1435.       && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
  1436.       /* zero_extract can apply to a QImode even if the bits extracted
  1437.          don't fit inside that byte.  In such a case, we may not do this
  1438.          optimization, since the OR or AND insn really would need
  1439.          to fit in a byte.  */
  1440.       && (INTVAL (XEXP (XEXP (x, 0), 1)) + INTVAL (XEXP (XEXP (x, 0), 2))
  1441.           < GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))))
  1442.     {
  1443.       int shiftcount;
  1444.       int newmask;
  1445. #ifdef BITS_BIG_ENDIAN
  1446.       shiftcount
  1447.         = GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  1448.           - INTVAL (XEXP (XEXP (x, 0), 1)) - INTVAL (XEXP (XEXP (x, 0), 2));
  1449. #else
  1450.       shiftcount
  1451.         = INTVAL (XEXP (XEXP (x, 0), 2));
  1452. #endif
  1453.       newmask = ((INTVAL (XEXP (XEXP (x, 1), 1)) << shiftcount)
  1454.              + (GET_CODE (XEXP (x, 1)) == AND
  1455.             ? (1 << shiftcount) - 1
  1456.             : 0));
  1457.       if (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  1458.           < HOST_BITS_PER_INT)
  1459.         newmask &= (1 << GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))) - 1;
  1460.       if (!undobuf.storage)
  1461.         undobuf.storage = (char *) oballoc (0);
  1462.       return
  1463.         gen_rtx (SET, VOIDmode,
  1464.              XEXP (XEXP (x, 0), 0),
  1465.              gen_rtx (GET_CODE (XEXP (x, 1)),
  1466.                   GET_MODE (XEXP (XEXP (x, 0), 0)),
  1467.                   XEXP (XEXP (XEXP (x, 1), 0), 0),
  1468.                   gen_rtx (CONST_INT, VOIDmode, newmask)));
  1469.     }
  1470.       /* Can simplify (set (cc0) (compare (zero/sign_extend FOO) CONST))
  1471.      to (set (cc0) (compare FOO CONST)) if CONST fits in FOO's mode
  1472.      and we are only testing equality.
  1473.      In fact, this is valid for zero_extend if what follows is an
  1474.      unsigned comparison, and for sign_extend with a signed comparison.  */
  1475.       if (SET_DEST (x) == cc0_rtx
  1476.       && GET_CODE (SET_SRC (x)) == COMPARE
  1477.       && (GET_CODE (XEXP (SET_SRC (x), 0)) == ZERO_EXTEND
  1478.           || GET_CODE (XEXP (SET_SRC (x), 0)) == SIGN_EXTEND)
  1479.       && next_insn_tests_no_inequality (subst_insn)
  1480.       && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
  1481.       /* This is overly cautious by one bit, but saves worrying about
  1482.          whether it is zero-extension or sign extension.  */
  1483.       && ((unsigned) INTVAL (XEXP (SET_SRC (x), 1))
  1484.           < (1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (SET_SRC (x), 0), 0))) - 1))))
  1485.     SUBST (XEXP (SET_SRC (x), 0), XEXP (XEXP (SET_SRC (x), 0), 0));
  1486.       break;
  1487.  
  1488.     case AND:
  1489.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  1490.     {
  1491.       rtx tem = simplify_and_const_int (x, to);
  1492.       if (tem)
  1493.         return tem;
  1494.     }
  1495.       break;
  1496.  
  1497.     case IOR:
  1498.     case XOR:
  1499.       /* (ior (ior x c1) c2) => (ior x c1|c2); likewise for xor.  */
  1500.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1501.       && GET_CODE (XEXP (x, 0)) == code
  1502.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  1503.     {
  1504.       int c0 = INTVAL (XEXP (x, 1));
  1505.       int c1 = INTVAL (XEXP (XEXP (x, 0), 1));
  1506.       int combined = (code == IOR ? c0 | c1 : c0 ^ c1);
  1507.  
  1508.       if (combined == 0)
  1509.         return XEXP (XEXP (x, 0), 0);
  1510.       if (!undobuf.storage)
  1511.         undobuf.storage = (char *) oballoc (0);
  1512.       SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, combined));
  1513.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1514.       break;
  1515.     }
  1516.  
  1517.     case FLOAT:
  1518.       /* (float (sign_extend <X>)) = (float <X>).  */
  1519.       if (was_replaced[0]
  1520.       && GET_CODE (to) == SIGN_EXTEND)
  1521.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1522.       break;
  1523.  
  1524.     case ZERO_EXTRACT:
  1525.       /* (ZERO_EXTRACT (TRUNCATE x)...)
  1526.      can become (ZERO_EXTRACT x ...).  */
  1527.       if (was_replaced[0]
  1528.       && GET_CODE (to) == TRUNCATE)
  1529.     {
  1530. #ifdef BITS_BIG_ENDIAN
  1531.       if (GET_CODE (XEXP (x, 2)) == CONST_INT)
  1532.         {
  1533.           if (!undobuf.storage)
  1534.         undobuf.storage = (char *) oballoc (0);
  1535.           /* On a big-endian machine, must increment the bit-number
  1536.          since sign bit is farther away in the pre-truncated value.  */
  1537.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1538.                   XEXP (to, 0),
  1539.                   XEXP (x, 1),
  1540.                   gen_rtx (CONST_INT, VOIDmode,
  1541.                        (INTVAL (XEXP (x, 2))
  1542.                     + GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))
  1543.                     - GET_MODE_BITSIZE (GET_MODE (to)))));
  1544.         }
  1545. #else
  1546.       SUBST (XEXP (x, 0), XEXP (to, 0));
  1547. #endif
  1548.     }
  1549.       /* Extracting a single bit from the result of a shift:
  1550.      see which bit it was before the shift and extract that directly.  */
  1551.       if (was_replaced[0]
  1552.       && (GET_CODE (to) == ASHIFTRT || GET_CODE (to) == LSHIFTRT
  1553.           || GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1554.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1555.       && XEXP (x, 1) == const1_rtx
  1556.       && GET_CODE (XEXP (x, 2)) == CONST_INT)
  1557.     {
  1558.       int shift = INTVAL (XEXP (to, 1));
  1559.       int newpos;
  1560.       if (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1561.         shift = - shift;
  1562. #ifdef BITS_BIG_ENDIAN
  1563.       shift = - shift;
  1564. #endif
  1565.       newpos = INTVAL (XEXP (x, 2)) + shift;
  1566.       if (newpos >= 0 &&
  1567.           newpos < GET_MODE_BITSIZE (GET_MODE (to)))
  1568.         {
  1569.           if (!undobuf.storage)
  1570.         undobuf.storage = (char *) oballoc (0);
  1571.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1572.                   XEXP (to, 0), const1_rtx,
  1573.                   gen_rtx (CONST_INT, VOIDmode, newpos));
  1574.         }
  1575.     }
  1576.       break;
  1577.  
  1578.     case LSHIFTRT:
  1579.     case ASHIFTRT:
  1580.     case ROTATE:
  1581.     case ROTATERT:
  1582. #ifdef SHIFT_COUNT_TRUNCATED
  1583.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1584.      True for all kinds of shifts and also for zero_extend.  */
  1585.       if (was_replaced[1]
  1586.       && (GET_CODE (to) == SIGN_EXTEND
  1587.           || GET_CODE (to) == ZERO_EXTEND)
  1588.       && FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
  1589.     {
  1590.       if (!undobuf.storage)
  1591.         undobuf.storage = (char *) oballoc (0);
  1592.       SUBST (XEXP (x, 1),
  1593.          /* This is a perverse SUBREG, wider than its base.  */
  1594.          gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
  1595.     }
  1596. #endif
  1597.       /* Two shifts in a row of same kind
  1598.      in same direction with constant counts
  1599.      may be combined.  */
  1600.       if (was_replaced[0]
  1601.       && GET_CODE (to) == GET_CODE (x)
  1602.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1603.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1604.       && INTVAL (XEXP (to, 1)) > 0
  1605.       && INTVAL (XEXP (x, 1)) > 0
  1606.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1607.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1608.     {
  1609.       if (!undobuf.storage)
  1610.         undobuf.storage = (char *) oballoc (0);
  1611.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1612.               XEXP (to, 0),
  1613.               gen_rtx (CONST_INT, VOIDmode,
  1614.                    INTVAL (XEXP (x, 1))
  1615.                    + INTVAL (XEXP (to, 1))));
  1616.     }
  1617.       break;
  1618.  
  1619.     case LSHIFT:
  1620.     case ASHIFT:
  1621. #ifdef SHIFT_COUNT_TRUNCATED
  1622.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1623.      True for all kinds of shifts and also for zero_extend.  */
  1624.       if (was_replaced[1]
  1625.       && (GET_CODE (to) == SIGN_EXTEND
  1626.           || GET_CODE (to) == ZERO_EXTEND)
  1627.       && GET_CODE (to) == REG)
  1628.     {
  1629.       if (!undobuf.storage)
  1630.         undobuf.storage = (char *) oballoc (0);
  1631.       SUBST (XEXP (x, 1), gen_rtx (SUBREG, GET_MODE (to), XEXP (to, 0), 0));
  1632.     }
  1633. #endif
  1634.       /* (lshift (and (lshiftrt <foo> <X>) <Y>) <X>)
  1635.      happens copying between bit fields in similar structures.
  1636.      It can be replaced by one and instruction.
  1637.      It does not matter whether the shifts are logical or arithmetic.  */
  1638.       if (GET_CODE (XEXP (x, 0)) == AND
  1639.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1640.       && INTVAL (XEXP (x, 1)) > 0
  1641.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1642.       && XEXP (XEXP (x, 0), 0) == to
  1643.       && (GET_CODE (to) == LSHIFTRT
  1644.           || GET_CODE (to) == ASHIFTRT)
  1645. #if 0
  1646. /* I now believe this restriction is unnecessary.
  1647.    The outer shift will discard those bits in any case, right?  */
  1648.  
  1649.           /* If inner shift is arithmetic, either it shifts left or
  1650.          the bits it shifts the sign into are zeroed by the and.  */
  1651.           && (INTVAL (XEXP (x, 1)) < 0
  1652.               || ((unsigned) INTVAL (XEXP (XEXP (x, 0), 1))
  1653.               < 1 << (GET_MODE_BITSIZE (GET_MODE (x))
  1654.                   - INTVAL (XEXP (x, 0)))))
  1655. #endif
  1656.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1657.       && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1658.     {
  1659.       if (!undobuf.storage)
  1660.         undobuf.storage = (char *) oballoc (0);
  1661.       /* The constant in the new `and' is <Y> << <X>
  1662.          but clear out all bits that don't belong in our mode.  */
  1663.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1664.               gen_rtx (CONST_INT, VOIDmode,
  1665.                    (GET_MODE_MASK (GET_MODE (x))
  1666.                     & ((GET_MODE_MASK (GET_MODE (x))
  1667.                     & INTVAL (XEXP (XEXP (x, 0), 1)))
  1668.                        << INTVAL (XEXP (x, 1))))));
  1669.     } 
  1670.       /* Two shifts in a row in same direction with constant counts
  1671.      may be combined.  */
  1672.       if (was_replaced[0]
  1673.       && (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1674.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1675.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1676.       && INTVAL (XEXP (to, 1)) > 0
  1677.       && INTVAL (XEXP (x, 1)) > 0
  1678.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1679.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1680.     {
  1681.       if (!undobuf.storage)
  1682.         undobuf.storage = (char *) oballoc (0);
  1683.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1684.               XEXP (to, 0),
  1685.               gen_rtx (CONST_INT, VOIDmode,
  1686.                    INTVAL (XEXP (x, 1))
  1687.                    + INTVAL (XEXP (to, 1))));
  1688.     }
  1689.       /* (ashift (ashiftrt <foo> <X>) <X>)
  1690.      (or, on some machines, (ashift (ashift <foo> <-X>) <X>) instead)
  1691.      happens if you divide by 2**N and then multiply by 2**N.
  1692.      It can be replaced by one `and' instruction.
  1693.      It does not matter whether the shifts are logical or arithmetic.  */
  1694.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1695.       && INTVAL (XEXP (x, 1)) > 0
  1696.       && was_replaced[0]
  1697.       && (((GET_CODE (to) == LSHIFTRT || GET_CODE (to) == ASHIFTRT)
  1698.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1699.            && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1700.           ||
  1701.           ((GET_CODE (to) == LSHIFT || GET_CODE (to) == ASHIFT)
  1702.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1703.            && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (to, 1)))))
  1704.     {
  1705.       if (!undobuf.storage)
  1706.         undobuf.storage = (char *) oballoc (0);
  1707.       /* The constant in the new `and' is -1 << <X>
  1708.          but clear out all bits that don't belong in our mode.  */
  1709.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1710.               gen_rtx (CONST_INT, VOIDmode,
  1711.                    (GET_MODE_MASK (GET_MODE (x))
  1712.                     & (GET_MODE_MASK (GET_MODE (x))
  1713.                        << INTVAL (XEXP (x, 1))))));
  1714.     } 
  1715.  
  1716.     }
  1717.  
  1718.   return x;
  1719. }
  1720.  
  1721. /* This is the AND case of the function subst.  */
  1722.  
  1723. static rtx
  1724. simplify_and_const_int (x, to)
  1725.      rtx x, to;
  1726. {
  1727.   register rtx varop = XEXP (x, 0);
  1728.   register int constop = INTVAL (XEXP (x, 1));
  1729.  
  1730.   /* (and (subreg (and <foo> <constant>) 0) <constant>)
  1731.      results from an andsi followed by an andqi,
  1732.      which happens frequently when storing bit-fields
  1733.      on something whose result comes from an andsi.  */
  1734.   if (GET_CODE (varop) == SUBREG
  1735.       && XEXP (varop, 0) == to
  1736.       && subreg_lowpart_p (varop)
  1737.       && GET_CODE (to) == AND
  1738.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1739.       /* Verify that the result of the outer `and'
  1740.      is not affected by any bits not defined in the inner `and'.
  1741.      True if the outer mode is narrower, or if the outer constant
  1742.      masks to zero all the bits that the inner mode doesn't have.  */
  1743.       && (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (to))
  1744.       || (constop & ~ GET_MODE_MASK (GET_MODE (to))) == 0))
  1745.     {
  1746.       if (!undobuf.storage)
  1747.     undobuf.storage = (char *) oballoc (0);
  1748.       return gen_rtx (AND, GET_MODE (x),
  1749.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1750.               gen_rtx (CONST_INT, VOIDmode,
  1751.                    constop
  1752.                    /* Remember that the bits outside that mode
  1753.                   are not being changed, so the effect
  1754.                   is as if they were all 1.  */
  1755.                    & INTVAL (XEXP (to, 1))));
  1756.     } 
  1757.   /* (and:SI (zero_extract:SI ...) <constant>)
  1758.      results from an andsi following a byte-fetch on risc machines.
  1759.      When the constant includes all bits extracted, eliminate the `and'.  */
  1760.   if (GET_CODE (varop) == ZERO_EXTRACT
  1761.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1762.       /* The `and' must not clear any bits that the extract can give.  */
  1763.       && (~ constop & ((1 << INTVAL (XEXP (varop, 1))) - 1)) == 0)
  1764.     return varop;
  1765.   /* (and (zero_extend <foo>) <constant>)
  1766.      often results from storing in a bit-field something
  1767.      that was calculated as a short.  Replace with a single `and'
  1768.      in whose constant all bits not in <foo>'s mode are zero.  */
  1769.   if (varop == to
  1770.       && GET_CODE (to) == ZERO_EXTEND
  1771.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1772.     {
  1773.       if (!undobuf.storage)
  1774.     undobuf.storage = (char *) oballoc (0);
  1775.       return gen_rtx (AND, GET_MODE (x),
  1776.               /* This is a perverse SUBREG, wider than its base.  */
  1777.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1778.               gen_rtx (CONST_INT, VOIDmode,
  1779.                    constop & GET_MODE_MASK (GET_MODE (XEXP (to, 0)))));
  1780.     }
  1781.   /* (and (sign_extend <foo>) <constant>)
  1782.      can be replaced with (and (subreg <foo>) <constant>)
  1783.      if <constant> is narrower than <foo>'s mode,
  1784.      or with (zero_extend <foo>) if <constant> is a mask for that mode.  */
  1785.   if (varop == to
  1786.       && GET_CODE (to) == SIGN_EXTEND
  1787.       && ((unsigned) constop <= GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1788.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1789.     {
  1790.       if (!undobuf.storage)
  1791.     undobuf.storage = (char *) oballoc (0);
  1792.       if (constop == GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1793.     return gen_rtx (ZERO_EXTEND, GET_MODE (x), XEXP (to, 0));
  1794.       return gen_rtx (AND, GET_MODE (x),
  1795.               /* This is a perverse SUBREG, wider than its base.  */
  1796.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1797.               XEXP (x, 1));
  1798.     }
  1799.   /* (and (and <foo> <constant>) <constant>)
  1800.      comes from two and instructions in a row.  */
  1801.   if (varop == to
  1802.       && GET_CODE (to) == AND
  1803.       && GET_CODE (XEXP (to, 1)) == CONST_INT)
  1804.     {
  1805.       if (!undobuf.storage)
  1806.     undobuf.storage = (char *) oballoc (0);
  1807.       return gen_rtx (AND, GET_MODE (x),
  1808.               XEXP (to, 0),
  1809.               gen_rtx (CONST_INT, VOIDmode,
  1810.                    constop
  1811.                    & INTVAL (XEXP (to, 1))));
  1812.     }
  1813.   /* (and (ashiftrt (ashift FOO N) N) CONST)
  1814.      may be simplified to (and FOO CONST) if CONST masks off the bits
  1815.      changed by the two shifts.  */
  1816.   if (GET_CODE (varop) == ASHIFTRT
  1817.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1818.       && XEXP (varop, 0) == to
  1819.       && GET_CODE (to) == ASHIFT
  1820.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1821.       && INTVAL (XEXP (varop, 1)) == INTVAL (XEXP (to, 1))
  1822.       && ((unsigned) constop >> INTVAL (XEXP (varop, 1))) == 0)
  1823.     {
  1824.       if (!undobuf.storage)
  1825.     undobuf.storage = (char *) oballoc (0);
  1826.       /* If CONST is a mask for the low byte,
  1827.      change this into a zero-extend instruction
  1828.      from just the low byte of FOO.  */
  1829.       if (constop == GET_MODE_MASK (QImode))
  1830.     {
  1831.       rtx temp = gen_lowpart_for_combine (QImode, XEXP (to, 0));
  1832.       if (GET_CODE (temp) != CLOBBER)
  1833.         return gen_rtx (ZERO_EXTEND, GET_MODE (x), temp);
  1834.     }
  1835.       return gen_rtx (AND, GET_MODE (x),
  1836.               XEXP (to, 0), XEXP (x, 1));
  1837.     }
  1838.   /* (and (ashiftrt (zero_extend FOO) N) CONST)
  1839.      may be simplified to (and (ashiftrt (subreg FOO) N) CONST)
  1840.      if CONST masks off the bits changed by extension.  */
  1841.   if ((GET_CODE (varop) == ASHIFTRT || GET_CODE (varop) == LSHIFTRT)
  1842.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1843.       && XEXP (varop, 0) == to
  1844.       && (GET_CODE (to) == ZERO_EXTEND || GET_CODE (to) == SIGN_EXTEND)
  1845.       /* Verify the and discards all the extended bits.  */
  1846.       && (((unsigned) constop << INTVAL (XEXP (varop, 1)))
  1847.       >> GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))) == 0
  1848.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1849.     {
  1850.       if (!undobuf.storage)
  1851.     undobuf.storage = (char *) oballoc (0);
  1852.       SUBST (XEXP (varop, 0),
  1853.          gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)));
  1854.       return x;
  1855.     }
  1856.   /* (and x const) may be converted to (zero_extend (subreg x 0)).  */
  1857.   if (constop == GET_MODE_MASK (QImode)
  1858.       && GET_CODE (varop) == REG)
  1859.     {
  1860.       if (!undobuf.storage)
  1861.     undobuf.storage = (char *) oballoc (0);
  1862.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1863.               gen_rtx (SUBREG, QImode, varop, 0));
  1864.     }
  1865.   if (constop == GET_MODE_MASK (HImode)
  1866.       && GET_CODE (varop) == REG)
  1867.     {
  1868.       if (!undobuf.storage)
  1869.     undobuf.storage = (char *) oballoc (0);
  1870.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1871.               gen_rtx (SUBREG, HImode, varop, 0));
  1872.     }
  1873.   /* No simplification applies.  */
  1874.   return 0;
  1875. }
  1876.  
  1877. /* Like gen_lowpart but for use by combine.  In combine it is not possible
  1878.    to create any new pseudoregs.  However, it is safe to create
  1879.    invalid memory addresses, because combine will try to recognize
  1880.    them and all they will do is make the combine attempt fail.
  1881.  
  1882.    If for some reason this cannot do its job, an rtx
  1883.    (clobber (const_int 0)) is returned.
  1884.    An insn containing that will not be recognized.  */
  1885.  
  1886. #undef gen_lowpart
  1887.  
  1888. static rtx
  1889. gen_lowpart_for_combine (mode, x)
  1890.      enum machine_mode mode;
  1891.      register rtx x;
  1892. {
  1893.   if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG)
  1894.     return gen_lowpart (mode, x);
  1895.   if (GET_MODE (x) == mode)
  1896.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1897.   if (GET_CODE (x) == MEM)
  1898.     {
  1899.       register int offset = 0;
  1900.  
  1901.       /* Refuse to work on a volatile memory ref.  */
  1902.       if (MEM_VOLATILE_P (x))
  1903.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1904.  
  1905.       /* If we want to refer to something bigger than the original memref,
  1906.      generate a perverse subreg instead.  That will force a reload
  1907.      of the original memref X.  */
  1908.       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
  1909.     return gen_rtx (SUBREG, mode, x, 0);
  1910.  
  1911. #ifdef WORDS_BIG_ENDIAN
  1912.       offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  1913.         - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  1914. #endif
  1915. #ifdef BYTES_BIG_ENDIAN
  1916.       /* Adjust the address so that the address-after-the-data
  1917.      is unchanged.  */
  1918.       offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  1919.          - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  1920. #endif
  1921.       return gen_rtx (MEM, mode, plus_constant (XEXP (x, 0),
  1922.                         offset));
  1923.     }
  1924.   else
  1925.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1926. }
  1927.  
  1928. /* After substitution, if the resulting pattern looks like
  1929.    (set (cc0) (and ...)) or (set (cc0) (lshiftrt ...)),
  1930.    this function is called to simplify the
  1931.    pattern into a bit-field operation if possible.  */
  1932.  
  1933. static void
  1934. simplify_set_cc0_and (insn)
  1935.      rtx insn;
  1936. {
  1937.   register rtx value = XEXP (PATTERN (insn), 1);
  1938.   register rtx op0 = XEXP (value, 0);
  1939.   register rtx op1 = XEXP (value, 1);
  1940.   int offset = 0;
  1941.   rtx var = 0;
  1942.   rtx bitnum = 0;
  1943.   int temp;
  1944.   int unit;
  1945.   rtx newpat;
  1946.  
  1947.   if (GET_CODE (value) == AND)
  1948.     {
  1949.       op0 = XEXP (value, 0);
  1950.       op1 = XEXP (value, 1);
  1951.     }
  1952.   else if (GET_CODE (value) == LSHIFTRT)
  1953.     {
  1954.       /* If there is no AND, but there is a shift that discards
  1955.      all but the sign bit, we can pretend that the shift result
  1956.      is ANDed with 1.  Otherwise we cannot handle just a shift.  */
  1957.       if (GET_CODE (XEXP (value, 1)) == CONST_INT
  1958.       && (INTVAL (XEXP (value, 1))
  1959.           == GET_MODE_BITSIZE (GET_MODE (value)) - 1))
  1960.     {
  1961.       op0 = value;
  1962.       op1 = const1_rtx;
  1963.     }
  1964.       else
  1965.     return;
  1966.     }
  1967.   else
  1968.     abort ();
  1969.  
  1970.   /* Look for a constant power of 2 or a shifted 1
  1971.      on either side of the AND.  Set VAR to the other side.
  1972.      Set BITNUM to the shift count of the 1 (as an rtx).
  1973.      Or, if bit number is constant, set OFFSET to the bit number.  */
  1974.  
  1975.   switch (GET_CODE (op0))
  1976.     {
  1977.     case CONST_INT:
  1978.       temp = exact_log2 (INTVAL (op0));
  1979.       if (temp < 0)
  1980.     return;
  1981.       offset = temp;
  1982.       var = op1;
  1983.       break;
  1984.  
  1985.     case ASHIFT:
  1986.     case LSHIFT:
  1987.       if (XEXP (op0, 0) == const1_rtx)
  1988.     {
  1989.       bitnum = XEXP (op0, 1);
  1990.       var = op1;
  1991.     }
  1992.     }
  1993.   if (var == 0)
  1994.     switch (GET_CODE (op1))
  1995.       {
  1996.       case CONST_INT:
  1997.     temp = exact_log2 (INTVAL (op1));
  1998.     if (temp < 0)
  1999.       return;
  2000.     offset = temp;
  2001.     var = op0;
  2002.     break;
  2003.  
  2004.       case ASHIFT:
  2005.       case LSHIFT:
  2006.     if (XEXP (op1, 0) == const1_rtx)
  2007.       {
  2008.         bitnum = XEXP (op1, 1);
  2009.         var = op0;
  2010.       }
  2011.       }
  2012.  
  2013.   /* If VAR is 0, we didn't find something recognizable.  */
  2014.   if (var == 0)
  2015.     return;
  2016.  
  2017.   if (!undobuf.storage)
  2018.     undobuf.storage = (char *) oballoc (0);
  2019.  
  2020.   /* If the bit position is currently exactly 0,
  2021.      extract a right-shift from the variable portion.  */
  2022.   if (offset == 0
  2023.       && (GET_CODE (var) == ASHIFTRT || GET_CODE (var) == LSHIFTRT))
  2024.     {
  2025.       bitnum = XEXP (var, 1);
  2026.       var = XEXP (var, 0);
  2027.     }
  2028.  
  2029.   if (GET_CODE (var) == SUBREG && SUBREG_WORD (var) == 0)
  2030.     var = SUBREG_REG (var);
  2031.  
  2032.   /* Note that BITNUM and OFFSET are always little-endian thru here
  2033.      even on a big-endian machine.  */
  2034.  
  2035. #ifdef BITS_BIG_ENDIAN
  2036.   unit = GET_MODE_BITSIZE (GET_MODE (var)) - 1;
  2037.  
  2038.   if (bitnum != 0)
  2039.     bitnum = gen_rtx (MINUS, SImode,
  2040.               gen_rtx (CONST_INT, VOIDmode, unit), bitnum);
  2041.   else
  2042.     offset = unit - offset;
  2043. #endif
  2044.  
  2045.   if (bitnum == 0)
  2046.     bitnum = gen_rtx (CONST_INT, VOIDmode, offset);
  2047.  
  2048.   newpat = gen_rtx (SET, VOIDmode, cc0_rtx,
  2049.             gen_rtx (ZERO_EXTRACT, VOIDmode, var, const1_rtx, bitnum));
  2050.   if (recog (newpat, insn) >= 0)
  2051.     {
  2052.       if (undobuf.num_undo < MAX_UNDO)
  2053.     {
  2054.       undobuf.undo[undobuf.num_undo].where = &XEXP (PATTERN (insn), 1);
  2055.       undobuf.undo[undobuf.num_undo].old_contents = value;
  2056.       XEXP (PATTERN (insn), 1) = XEXP (newpat, 1);
  2057.     }
  2058.       undobuf.num_undo++;
  2059.     }
  2060. }
  2061.  
  2062. /* Update the records of when each REG was most recently set or killed
  2063.    for the things done by INSN.  This is the last thing done in processing
  2064.    INSN in the combiner loop.
  2065.  
  2066.    We update reg_last_set, reg_last_death, and also the similar information
  2067.    mem_last_set (which insn most recently modified memory)
  2068.    and last_call_cuid (which insn was the most recent subroutine call).  */
  2069.  
  2070. static void
  2071. record_dead_and_set_regs (insn)
  2072.      rtx insn;
  2073. {
  2074.   register rtx link;
  2075.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  2076.     {
  2077.       if (REG_NOTE_KIND (link) == REG_DEAD)
  2078.     reg_last_death[REGNO (XEXP (link, 0))] = insn;
  2079.       else if (REG_NOTE_KIND (link) == REG_INC)
  2080.     reg_last_set[REGNO (XEXP (link, 0))] = insn;
  2081.     }
  2082.  
  2083.   if (GET_CODE (insn) == CALL_INSN)
  2084.     last_call_cuid = mem_last_set = INSN_CUID (insn);
  2085.  
  2086.   if (GET_CODE (PATTERN (insn)) == PARALLEL)
  2087.     {
  2088.       register int i;
  2089.       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  2090.     {
  2091.       register rtx elt = XVECEXP (PATTERN (insn), 0, i);
  2092.       register enum rtx_code code = GET_CODE (elt);
  2093.       if (code == SET || code == CLOBBER)
  2094.         {
  2095.           rtx dest = XEXP (elt, 0);
  2096.           while (GET_CODE (dest) == SUBREG
  2097.              || GET_CODE (dest) == STRICT_LOW_PART
  2098.              || GET_CODE (dest) == SIGN_EXTRACT
  2099.              || GET_CODE (dest) == ZERO_EXTRACT)
  2100.         dest = XEXP (dest, 0);
  2101.           
  2102.           if (GET_CODE (dest) == REG)
  2103.         reg_last_set[REGNO (dest)] = insn;
  2104.           else if (GET_CODE (dest) == MEM)
  2105.         mem_last_set = INSN_CUID (insn);
  2106.         }
  2107.     }
  2108.     }
  2109.   else if (GET_CODE (PATTERN (insn)) == SET
  2110.        || GET_CODE (PATTERN (insn)) == CLOBBER)
  2111.     {
  2112.       register rtx dest = XEXP (PATTERN (insn), 0);
  2113.  
  2114.       while (GET_CODE (dest) == SUBREG
  2115.          || GET_CODE (dest) == STRICT_LOW_PART
  2116.          || GET_CODE (dest) == SIGN_EXTRACT
  2117.          || GET_CODE (dest) == ZERO_EXTRACT)
  2118.     dest = XEXP (dest, 0);
  2119.  
  2120.       if (GET_CODE (dest) == REG)
  2121.     reg_last_set[REGNO (dest)] = insn;
  2122.       else if (GET_CODE (dest) == MEM)
  2123.     mem_last_set = INSN_CUID (insn);
  2124.     }
  2125. }
  2126.  
  2127. /* Return nonzero if expression X refers to a REG or to memory
  2128.    that is set in an instruction more recent than FROM_CUID.  */
  2129.  
  2130. static int
  2131. use_crosses_set_p (x, from_cuid)
  2132.      register rtx x;
  2133.      int from_cuid;
  2134. {
  2135.   register char *fmt;
  2136.   register int i;
  2137.   register enum rtx_code code = GET_CODE (x);
  2138.  
  2139.   if (code == REG)
  2140.     {
  2141.       register int regno = REGNO (x);
  2142. #ifdef PUSH_ROUNDING
  2143.       /* Don't allow uses of the stack pointer to be moved,
  2144.      because we don't know whether the move crosses a push insn.  */
  2145.       if (regno == STACK_POINTER_REGNUM)
  2146.     return 1;
  2147. #endif
  2148.       return (reg_last_set[regno]
  2149.           && INSN_CUID (reg_last_set[regno]) > from_cuid);
  2150.     }
  2151.  
  2152.   if (code == MEM && mem_last_set > from_cuid)
  2153.     return 1;
  2154.  
  2155.   fmt = GET_RTX_FORMAT (code);
  2156.  
  2157.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2158.     {
  2159.       if (fmt[i] == 'E')
  2160.     {
  2161.       register int j;
  2162.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2163.         if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
  2164.           return 1;
  2165.     }
  2166.       else if (fmt[i] == 'e'
  2167.            && use_crosses_set_p (XEXP (x, i), from_cuid))
  2168.     return 1;
  2169.     }
  2170.   return 0;
  2171. }
  2172.  
  2173. /* Return nonzero if reg REGNO is marked as dying in INSN.  */
  2174.  
  2175. int
  2176. regno_dead_p (regno, insn)
  2177.      int regno;
  2178.      rtx insn;
  2179. {
  2180.   register rtx link;
  2181.  
  2182.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  2183.     if ((REG_NOTE_KIND (link) == REG_DEAD
  2184.      || REG_NOTE_KIND (link) == REG_INC)
  2185.     && REGNO (XEXP (link, 0)) == regno)
  2186.       return 1;
  2187.  
  2188.   return 0;
  2189. }
  2190.  
  2191. /* Return nonzero if J is the first insn following I,
  2192.    not counting labels, line numbers, etc.
  2193.    We assume that J follows I.  */
  2194.  
  2195. static int
  2196. adjacent_insns_p (i, j)
  2197.      rtx i, j;
  2198. {
  2199.   register rtx insn;
  2200.   for (insn = NEXT_INSN (i); insn != j; insn = NEXT_INSN (insn))
  2201.     if (GET_CODE (insn) == INSN
  2202.     || GET_CODE (insn) == CALL_INSN
  2203.     || GET_CODE (insn) == JUMP_INSN)
  2204.       return 0;
  2205.   return 1;
  2206. }
  2207.  
  2208. /* Check that X is an insn-body for an `asm' with operands
  2209.    and that the operands mentioned in it are legitimate.  */
  2210.  
  2211. static int
  2212. check_asm_operands (x)
  2213.      rtx x;
  2214. {
  2215.   int noperands = asm_noperands (x);
  2216.   rtx *operands;
  2217.   int i;
  2218.  
  2219.   if (noperands < 0)
  2220.     return 0;
  2221.   if (noperands == 0)
  2222.     return 1;
  2223.  
  2224.   operands = (rtx *) alloca (noperands * sizeof (rtx));
  2225.   decode_asm_operands (x, operands, 0, 0, 0);
  2226.  
  2227.   for (i = 0; i < noperands; i++)
  2228.     if (!general_operand (operands[i], VOIDmode))
  2229.       return 0;
  2230.  
  2231.   return 1;
  2232. }
  2233.  
  2234. /* Concatenate the list of logical links of OINSN
  2235.    into INSN's list of logical links.
  2236.    Modifies OINSN destructively.
  2237.  
  2238.    If ALL_LINKS is nonzero, move all the links that OINSN has.
  2239.    Otherwise, move only those that point to insns that set regs
  2240.    that die in the insn OINSN.
  2241.    Other links are clobbered so that they are no longer effective.  */
  2242.  
  2243. static void
  2244. add_links (insn, oinsn, all_links)
  2245.      rtx insn, oinsn;
  2246.      int all_links;
  2247. {
  2248.   register rtx links = LOG_LINKS (oinsn);
  2249.   if (! all_links)
  2250.     {
  2251.       rtx tail;
  2252.       for (tail = links; tail; tail = XEXP (tail, 1))
  2253.     {
  2254.       rtx target = XEXP (tail, 0);
  2255.       if (GET_CODE (target) != INSN
  2256.           || GET_CODE (PATTERN (target)) != SET
  2257.           || GET_CODE (SET_DEST (PATTERN (target))) != REG
  2258.           || ! dead_or_set_p (oinsn, SET_DEST (PATTERN (target))))
  2259.         /* OINSN is going to become a NOTE 
  2260.            so a link pointing there will have no effect.  */
  2261.         XEXP (tail, 0) = oinsn;
  2262.     }
  2263.     }
  2264.   if (LOG_LINKS (insn) == 0)
  2265.     LOG_LINKS (insn) = links;
  2266.   else
  2267.     {
  2268.       register rtx next, prev = LOG_LINKS (insn);
  2269.       while (next = XEXP (prev, 1))
  2270.     prev = next;
  2271.       XEXP (prev, 1) = links;
  2272.     }
  2273. }
  2274.   
  2275. /* Delete any LOG_LINKS of INSN which point at OINSN.  */
  2276.  
  2277. static void
  2278. remove_links (insn, oinsn)
  2279.      rtx insn, oinsn;
  2280. {
  2281.   register rtx next = LOG_LINKS (insn), prev = 0;
  2282.   while (next)
  2283.     {
  2284.       if (XEXP (next, 0) == oinsn)
  2285.     {
  2286.       if (prev)
  2287.         XEXP (prev, 1) = XEXP (next, 1);
  2288.       else
  2289.         LOG_LINKS (insn) = XEXP (next, 1);
  2290.     }
  2291.       else
  2292.     prev = next;
  2293.       next = XEXP (next, 1);
  2294.     }
  2295. }
  2296.  
  2297. /* Concatenate the any elements of the list of reg-notes INCS
  2298.    which are of type REG_INC
  2299.    into INSN's list of reg-notes.  */
  2300.  
  2301. static void
  2302. add_incs (insn, incs)
  2303.      rtx insn, incs;
  2304. {
  2305.   register rtx tail;
  2306.  
  2307.   for (tail = incs; tail; tail = XEXP (tail, 1))
  2308.     if (REG_NOTE_KIND (tail) == REG_INC)
  2309.       REG_NOTES (insn)
  2310.     = gen_rtx (EXPR_LIST, REG_INC, XEXP (tail, 0), REG_NOTES (insn));
  2311. }
  2312.  
  2313. /* Remove register number REGNO from the dead registers list of INSN.  */
  2314.  
  2315. void
  2316. remove_death (regno, insn)
  2317.      int regno;
  2318.      rtx insn;
  2319. {
  2320.   register rtx link, next;
  2321.   while ((link = REG_NOTES (insn))
  2322.      && REG_NOTE_KIND (link) == REG_DEAD
  2323.      && REGNO (XEXP (link, 0)) == regno)
  2324.     REG_NOTES (insn) = XEXP (link, 1);
  2325.  
  2326.   if (link)
  2327.     while (next = XEXP (link, 1))
  2328.       {
  2329.     if (REG_NOTE_KIND (next) == REG_DEAD
  2330.         && REGNO (XEXP (next, 0)) == regno)
  2331.       XEXP (link, 1) = XEXP (next, 1);
  2332.     else
  2333.       link = next;
  2334.       }
  2335. }
  2336.  
  2337. /* For each register (hardware or pseudo) used within expression X,
  2338.    if its death is in an instruction with cuid
  2339.    between FROM_CUID (inclusive) and TO_INSN (exclusive),
  2340.    mark it as dead in TO_INSN instead.
  2341.  
  2342.    This is done when X is being merged by combination into TO_INSN.  */
  2343.  
  2344. static void
  2345. move_deaths (x, from_cuid, to_insn)
  2346.      rtx x;
  2347.      int from_cuid;
  2348.      rtx to_insn;
  2349. {
  2350.   register char *fmt;
  2351.   register int len, i;
  2352.   register enum rtx_code code = GET_CODE (x);
  2353.  
  2354.   if (code == REG)
  2355.     {
  2356.       register rtx where_dead = reg_last_death[REGNO (x)];
  2357.  
  2358.       if (where_dead && INSN_CUID (where_dead) >= from_cuid
  2359.       && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  2360.     {
  2361.       remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  2362.       if (! dead_or_set_p (to_insn, x))
  2363.         REG_NOTES (to_insn)
  2364.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2365.     }
  2366.       return;
  2367.     }
  2368.  
  2369.   len = GET_RTX_LENGTH (code);
  2370.   fmt = GET_RTX_FORMAT (code);
  2371.  
  2372.   for (i = 0; i < len; i++)
  2373.     {
  2374.       if (fmt[i] == 'E')
  2375.     {
  2376.       register int j;
  2377.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2378.         move_deaths (XVECEXP (x, i, j), from_cuid, to_insn);
  2379.     }
  2380.       else if (fmt[i] == 'e')
  2381.     move_deaths (XEXP (x, i), from_cuid, to_insn);
  2382.     }
  2383. }
  2384.  
  2385. /* Like move_deaths, but deaths are moving both forward
  2386.    (from FROM_CUID to TO_INSN), and backwards
  2387.    (from FROM_INSN to TO_INSN).  This is what happens
  2388.    when an insn is removed after applying the distributive law.  */
  2389.  
  2390. static void
  2391. move_deaths_2 (x, from_cuid, from_insn, to_insn)
  2392.      rtx x;
  2393.      int from_cuid;
  2394.      rtx from_insn, to_insn;
  2395. {
  2396.   register char *fmt;
  2397.   register int len, i;
  2398.   register enum rtx_code code = GET_CODE (x);
  2399.  
  2400.   if (code == REG)
  2401.     {
  2402.       register rtx where_dead = reg_last_death[REGNO (x)];
  2403.  
  2404.       if (where_dead && INSN_CUID (where_dead) >= from_cuid
  2405.       && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  2406.     {
  2407.       remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  2408.       if (! dead_or_set_p (to_insn, x))
  2409.         REG_NOTES (to_insn)
  2410.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2411.     }
  2412.       /* Can't use where_dead for from_insn because it has
  2413.      not been computed yet.  */
  2414.       else if (dead_or_set_p (from_insn, x))
  2415.     {
  2416.       remove_death (REGNO (x), from_insn);
  2417.       if (! dead_or_set_p (to_insn, x))
  2418.         REG_NOTES (to_insn)
  2419.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2420.     }
  2421.       return;
  2422.     }
  2423.  
  2424.   len = GET_RTX_LENGTH (code);
  2425.   fmt = GET_RTX_FORMAT (code);
  2426.  
  2427.   for (i = 0; i < len; i++)
  2428.     {
  2429.       if (fmt[i] == 'E')
  2430.     {
  2431.       register int j;
  2432.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2433.         move_deaths_2 (XVECEXP (x, i, j), from_cuid, from_insn, to_insn);
  2434.     }
  2435.       else if (fmt[i] == 'e')
  2436.     move_deaths_2 (XEXP (x, i), from_cuid, from_insn, to_insn);
  2437.     }
  2438. }
  2439.  
  2440. /* The distrib combiner rewrites groups of insns so that optimizations
  2441.    can be more easily recognized.  The front-end does not know how to
  2442.    group certain kinds of operations for efficient execution, and the
  2443.    resulting code can be quite poor.  For example, on a machine without
  2444.    bitfield instructions, bitfield references look like
  2445.  
  2446.     (and (lshiftrt ... n) m)
  2447.  
  2448.    When combining two bitfield operations, such as with ||, this can
  2449.    yield code like
  2450.  
  2451.     (set z
  2452.          (or (and (lshiftrt x n) 1)
  2453.          (and (lshiftrt y n) 1)))
  2454.  
  2455.    which can be more efficiently executed as
  2456.  
  2457.     (set z
  2458.          (lshiftrt (and (or x y)
  2459.                 (1 << m)) n))
  2460.  
  2461.    From there, the combiner attempts to rewrite the insns,
  2462.    keeping flow information accurate for later passes,
  2463.    and reducing the total number of insns executed.
  2464.  
  2465.    This function returns the point at which we should try
  2466.    looking for more simplifications.  This will be before
  2467.    INSN if the call succeeds.  We do not need to fear
  2468.    infinite loops, since this function is guaranteed to
  2469.    eliminate at least one (non-note) instruction if it returns
  2470.    successfully.  */
  2471.  
  2472. static rtx
  2473. try_distrib (insn, xprev1, xprev2)
  2474.      rtx insn, xprev1, xprev2;
  2475. {
  2476.   rtx pat = PATTERN (insn);
  2477.   rtx prev1, prev2, pat1, pat2, src1, src2;
  2478.   rtx to_prev, to_insn;
  2479.   enum rtx_code code;
  2480.   int insn_code_number, prev_code_number, regno;
  2481.   rtx new_insn_pat, new_prev_pat;
  2482.  
  2483.   distrib_attempts++;
  2484.  
  2485.   /* ??? Need to implement a test that PREV2 and PREV1
  2486.      are completely independent.  Right now their
  2487.      recognition ability is sufficiently limited that
  2488.      it should not be necessary, but better safe than sorry.  */
  2489.  
  2490.   /* Let PREV1 be the later of the two insns, and PREV2 the earlier.  */
  2491.   if (INSN_CUID (xprev1) > INSN_CUID (xprev2))
  2492.     {
  2493.       prev1 = xprev1;
  2494.       prev2 = xprev2;
  2495.     }
  2496.   else
  2497.     {
  2498.       prev1 = xprev2;
  2499.       prev2 = xprev1;
  2500.     }
  2501.  
  2502.   pat1 = PATTERN (prev1);
  2503.   pat2 = PATTERN (prev2);
  2504.  
  2505.   /* First, see if INSN, PREV1, and PREV2 have patterns we can expect
  2506.      to simplify.  */
  2507.  
  2508.   if (GET_CODE (pat) != SET
  2509.       || GET_CODE (pat1) != SET
  2510.       || GET_CODE (pat2) != SET)
  2511.     return 0;
  2512.  
  2513.   code = GET_CODE (SET_SRC (pat));
  2514.   src1 = SET_SRC (pat1);
  2515.   src2 = SET_SRC (pat2);
  2516.  
  2517.   if (GET_CODE (SET_DEST (pat1)) != REG
  2518.       || GET_CODE (SET_DEST (pat2)) != REG)
  2519.     return 0;
  2520.  
  2521.   switch (code)
  2522.     {
  2523.     default:
  2524.       return 0;
  2525.  
  2526.     case IOR:
  2527.     case AND:
  2528.     case XOR:
  2529.     case PLUS:
  2530.       ;
  2531.     }
  2532.  
  2533.   /* Insns PREV1 and PREV2 must provide the two operands of the arithmetic
  2534.      that is done in INSN.  */
  2535.   if (! ((XEXP (SET_SRC (pat), 0) == SET_DEST (pat1)
  2536.       && XEXP (SET_SRC (pat), 1) == SET_DEST (pat2))
  2537.      ||
  2538.      (XEXP (SET_SRC (pat), 0) == SET_DEST (pat2)
  2539.       && XEXP (SET_SRC (pat), 1) == SET_DEST (pat1))))
  2540.     return 0;
  2541.  
  2542.   /* They must not be used in any other way in INSN.
  2543.      In particular, they must not be used in a result memory address.  */
  2544.   if (reg_mentioned_p (SET_DEST (pat1), SET_DEST (pat))
  2545.       || reg_mentioned_p (SET_DEST (pat2), SET_DEST (pat)))
  2546.     return 0;
  2547.  
  2548.   /* Give up if the two operands' modes don't match.  */
  2549.   if (GET_MODE (src1) != GET_MODE (src2))
  2550.     return 0;
  2551.  
  2552.   /* PREV1 and PREV2 must compute the same operation.
  2553.      Actually, there are other cases that could be handled,
  2554.      but are not implemented.  For example:
  2555.  
  2556.      (set (reg:SI 94)
  2557.       (and:SI (reg:SI 73)
  2558.           (const_int 223)))
  2559.  
  2560.      (set (reg:SI 95)
  2561.       (zero_extend:SI (subreg:QI (reg:SI 91) 0)))
  2562.  
  2563.      (set (reg:SI 96)
  2564.       (ior:SI (reg:SI 94)
  2565.           (reg:SI 95)))
  2566.  
  2567.      In this case, we know that because (reg:SI 94) has
  2568.      been anded with 223, there is no need to zero_extend
  2569.      (reg:SI 91), and we could eliminate (reg:SI 95).  */
  2570.  
  2571.   if (GET_CODE (src1) != GET_CODE (src2))
  2572.     return 0;
  2573.  
  2574.   /* The SETs in PREV1 and PREV2 do not need to be kept around.  */
  2575.  
  2576.   undobuf.num_undo = 0;
  2577.   undobuf.storage = 0;
  2578.  
  2579.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  2580.   subst_insn = insn;
  2581.   n_occurrences = 0;    /* `subst' counts here */
  2582.  
  2583.   switch (GET_CODE (src1))
  2584.     {
  2585.     /* case XOR:  Does not distribute through anything!  */
  2586.     case LSHIFTRT:
  2587.     case ASHIFTRT:
  2588.       /* Right-shift can't distribute through addition
  2589.      since the round-off would happen differently.  */
  2590.     case AND:
  2591.     case IOR:
  2592.       /* Boolean ops don't distribute through addition.  */
  2593.       if (code == PLUS)
  2594.     return 0;
  2595.  
  2596.     case LSHIFT:
  2597.     case ASHIFT:
  2598.       /* Left shifts are multiplication; they distribute through
  2599.      addition.  Also, since they work bitwise, they
  2600.      distribute through boolean operations.  */
  2601.       goto do_distrib;
  2602.  
  2603.     case MULT:
  2604.       /* Multiplication distributes through addition only.  */
  2605.       if (code != PLUS)
  2606.     return 0;
  2607.  
  2608.     do_distrib:
  2609.       /* Try changing (+ (* x c) (* y c)) to (* (+ x y) c).  */
  2610.  
  2611.       if (GET_CODE (XEXP (src1, 1)) != CONST_INT
  2612.       || GET_CODE (XEXP (src2, 1)) != CONST_INT
  2613.       || INTVAL (XEXP (src1, 1)) != INTVAL (XEXP (src2, 1)))
  2614.     return 0;
  2615.       to_prev = gen_rtx (code, GET_MODE (src1),
  2616.              XEXP (src1, 0), XEXP (src2, 0));
  2617.       to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1), SET_DEST (pat1), XEXP (src1, 1));
  2618.       break;
  2619.  
  2620.     case ZERO_EXTEND:
  2621.     case SIGN_EXTEND:
  2622.       /* Extension can't distribute through addition;
  2623.      the carries could be changed.  */
  2624.       if (code == PLUS)
  2625.     return 0;
  2626.       {
  2627.     rtx inner1 = XEXP (src1, 0), inner2 = XEXP (src2, 0);
  2628.     int subreg_needed = 0;
  2629.  
  2630.     /* Try changing (+ (extend x) (extend y)) to (extend (+ x y)).  */
  2631.     /* But keep extend insns together with their subregs.  */
  2632.     if (GET_CODE (inner1) == SUBREG)
  2633.       if (SUBREG_WORD (inner1) != 0)
  2634.         return 0;
  2635.       else
  2636.         {
  2637.           subreg_needed = 1;
  2638.           inner1 = SUBREG_REG (inner1);
  2639.         }
  2640.  
  2641.     if (GET_CODE (inner2) == SUBREG)
  2642.       if (SUBREG_WORD (inner2) != 0)
  2643.         return 0;
  2644.       else
  2645.         {
  2646.           subreg_needed = 1;
  2647.           inner2 = SUBREG_REG (inner2);
  2648.         }
  2649.  
  2650.     to_prev = gen_rtx (code, GET_MODE (src1), inner1, inner2);
  2651.     to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1),
  2652.                subreg_needed
  2653.                ? gen_rtx (SUBREG, GET_MODE (XEXP (src1, 0)),
  2654.                       SET_DEST (pat1), 0)
  2655.                : SET_DEST (pat1));
  2656.       }
  2657.       break;
  2658.  
  2659.     default:
  2660.       return 0;
  2661.     }
  2662.  
  2663.   /* Are the results of this "substitution" a valid instruction?  */
  2664.  
  2665.   new_insn_pat = subst (PATTERN (insn), SET_SRC (PATTERN (insn)), to_insn);
  2666.   distrib_merges_1++;
  2667.  
  2668.   insn_code_number = recog (new_insn_pat, insn);
  2669.   if (insn_code_number < 0)
  2670.     {
  2671.       undo_all ();
  2672.       return 0;
  2673.     }
  2674.  
  2675.   subst_insn = prev1;
  2676.   new_prev_pat = subst (pat1, src1, to_prev);
  2677.   distrib_merges_2++;
  2678.  
  2679.   prev_code_number = recog (new_prev_pat, prev1);
  2680.   if (prev_code_number < 0)
  2681.     {
  2682.       undo_all ();
  2683.       return 0;
  2684.     }
  2685.  
  2686.   /* Everything worked; install the new patterns.  */
  2687.   INSN_CODE (insn) = insn_code_number;
  2688.   PATTERN (insn) = new_insn_pat;
  2689.  
  2690.   INSN_CODE (prev1) = prev_code_number;
  2691.   PATTERN (prev1) = new_prev_pat;
  2692.  
  2693.   /* Need to change LOG_LINKS around...PREV1 now gets
  2694.      whatever flowed into PREV2.  PREV2 is going to
  2695.      become a NOTE, so we clear out its LOG_LINKS.  */
  2696.   remove_links (insn, prev2);
  2697.   add_links (prev1, prev2, adjacent_insns_p (prev2, prev1));
  2698.  
  2699.   /* Registers which died in PREV2 now die in PREV1.
  2700.      Also, registers born in PREV2 dying in INSN now die in PREV1.  */
  2701.   move_deaths_2 (src2, INSN_CUID (prev2), insn, prev1);
  2702.  
  2703.   regno = REGNO (SET_DEST (pat2));
  2704.  
  2705.   reg_n_sets[regno]--;
  2706.   if (reg_n_sets[regno] == 0
  2707.       && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  2708.         & (1 << (regno % HOST_BITS_PER_INT))))
  2709.     reg_n_refs[regno] = 0;
  2710.   remove_death (regno, insn);
  2711.  
  2712.   PUT_CODE (prev2, NOTE);
  2713.   NOTE_LINE_NUMBER (prev2) = NOTE_INSN_DELETED;
  2714.   NOTE_SOURCE_FILE (prev2) = 0;
  2715.  
  2716.   distrib_successes++;
  2717.   return prev1;
  2718. }
  2719.  
  2720. void
  2721. dump_combine_stats (file)
  2722.      FILE *file;
  2723. {
  2724.   fprintf
  2725.     (file,
  2726.      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
  2727.      combine_attempts, combine_merges, combine_extras, combine_successes);
  2728.   fprintf
  2729.     (file,
  2730.      ";; Distributer statistics: %d attempts, %d:%d substitutions,\n;; %d successes.\n\n",
  2731.      distrib_attempts, distrib_merges_1,
  2732.      distrib_merges_2, distrib_successes);
  2733. }
  2734.  
  2735. void
  2736. dump_combine_total_stats (file)
  2737.      FILE *file;
  2738. {
  2739.   fprintf
  2740.     (file,
  2741.      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  2742.      total_attempts, total_merges, total_extras, total_successes);
  2743.   fprintf
  2744.     (file,
  2745.      "\n;; Distributer totals: %d attempts, %d:%d substitutions,\n;; %d successes.\n",
  2746.      total_distrib_attempts, total_distrib_merges_1,
  2747.      total_distrib_merges_2, total_distrib_successes);
  2748. }
  2749.